Simple implementation of size ordering in Java string array _java

Source: Internet
Author: User

If you compare the two strings directly to the size, the package: the operator > is undefined for the argument type (s) java.lang.String, java.lang.String error.

The string comparison size can be either string length or the ASCII value of the character in the comparison string, which is too simple to tell the record.

Strings are compared in ASCII size, and the rules are:

1, compare the first letter of the ASCII code size

2, if the previous letter is the same, then the comparison of the letter after the ASCII code value

3, if a string from the beginning of the first letter contains another string, the length of the string is considered large; example: ABC > AB

Note: Using Commons-logging-1.2.jar in code, sorting from small to large

Import Org.apache.commons.logging.Log;

Import Org.apache.commons.logging.LogFactory; /** * Sort the array of strings * @author Panjianghong * @since 2016/8/31 */public class Stringsort {private static final Lo
  G _log = Logfactory.getlog (Stringsort.class); /** * Sort string array * @param keys * @return */public static string[] Geturlparam (string[] keys) {f  or (int i = 0; i < keys.length-1 i++) {for (int j = 0; J < keys.length-i-1; + +) {String pre =
        KEYS[J];
        String next = keys[j + 1];
          if (Ismorethan (pre, Next)) {String temp = pre;
          KEYS[J] = next;
        KEYS[J+1] = temp;
  }} return keys; /** * Compares the size of two strings, comparing the ASCII code by letter * @param PRE * @param next * * * * * * * * @return */private static Boolean Ismor EThan (string Pre, String next) {if (null = PRE | |) NULL = NEXT | | "". Equals (PRE) | | "". Equals (next) {_log.error ("string comparison data cannot be empty!")
      ");
    return false;
    
}    char[] C_pre = Pre.tochararray ();
    
    char[] C_next = Next.tochararray ();
    
    int minsize = Math.min (C_pre.length, c_next.length);
      for (int i = 0; i < minsize i++) {if (int) c_pre[i] > (int) c_next[i]) {return true;
      }else if ((int) c_pre[i] < (int) c_next[i]) {return false;
    } if (C_pre.length > C_next.length) {return true;
  return false; public static void Main (string[] args) {string[] keys = geturlparam (new string[]{"Fin", "abc", "Shidema"
    
    , "Shide", "Bushi"});
    for (String Key:keys) {System.out.println (key); }

  }
}

The console prints the result:

Abc
Bushi
Fin
Shide
Shidema

The above Java string array for the size of the simple implementation of the order is to share the whole of the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.