Java string sort Chinese + digit

Source: Internet
Author: User
Tags collator comparable locale sort wrapper

idea: In Java, the sort needs to be overwritten by the Equals method and the public int compareTo (T o) of the comparable<t> interface;

Steps:

1. Use regular expressions to judge numbers, multiple consecutive numbers as a group,

2. Once a digital combination is retrieved,

3. Check out the next set of numbers and, if so, go to step 4, or go to step 6.

4. If the two sets of numbers appear equal, and the strings in the previous section are equal, enter step 5th. Otherwise break, skip to step 6th.

5. If the string in the previous section is exactly the same. Compares the size of two digits and, if the size is the same, goes to the next group, which is step 3. If the size is inconsistent, you can compare it to the size and end the comparison.

6. Call the CompareTo method of string, and the disease returns (the process ends).
The complete code is as follows:

Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern;
	Wrapper class public class Orderwrapper implements comparable&lt;orderwrapper&gt;{String name = null;
	Public Orderwrapper (String name) {this.name = name;
	Public String GetName () {return name;
	public void SetName (String name) {this.name = name;
	@Override public String toString () {return string.valueof (name);
		@Override public boolean equals (Object obj) {if (obj = =) {return true;
	
			} if (obj instanceof orderwrapper) {orderwrapper other = (orderwrapper) obj;
			if (null = = THIS.name) {return false;
			else {return this.name.equals (other.name);
	return false; }//comparison method, which is equivalent to subtraction.
		(return this-wrapper) public int compareTo (Orderwrapper wrapper) {if (null = = wrapper) {return 1;
		}//Direct equality if (this = = Wrapper | | this.equals (wrapper)) {return 0; } String name1 = this.Name
		String name2 = wrapper.name;
		In a special case, name has an empty condition.
			if (null = = name1) {//are NULL, think relative if (null = = name2) {return 0;
			else {return-1;
		} else if (null = = name2) {return 1;
		}//Middle 1-Multiple digital pattern pattern = Pattern.compile ("d* (d+) d*");
		Matcher matcher1 = Pattern.matcher (name1);
		Matcher matcher2 = Pattern.matcher (name2);
		System.out.println (Pattern.pattern ());
		int index1_step = 0;
		int index2_step = 0;
			while (Matcher1.find ()) {String S1 = matcher1.group (1);
			String s2 = null;
			if (Matcher2.find ()) {s2 = Matcher2.group (1);
			int index1 = Name1.indexof (S1, index1_step);
			int index2 = name2.indexof (s2, index2_step);
			Index1_step = index1;
			Index2_step = Index2;
				If the index is equal, if (index1 = = index2) {System.out.println ("name1=" +name1.length () + "nname2=" +name2.length ());
				System.out.println ("index1=" +index1+ ", index2=" +index2);
				String pre1 = name1.substring (0, index1); String Pre2 = Name2.substrinG (0, Index2);
					if (Pre1.equals (Pre2)) {//Long NUM1 = Long.parselong (S1);
					Long num2 = Long.parselong (s2);
					if (NUM1 = = num2) {//Compare the next group of continue;
					else {return (int) (NUM1-NUM2);
				} else {break;
			} else {break;
		}//Last case.
	Return This.name.compareTo (Wrapper.name);  
        public static void Testnew () {list&lt;orderwrapper&gt; chinesesorderlist = new arraylist&lt;orderwrapper&gt; ();  
        Chinesesorderlist.add (The new Orderwrapper ("We Are the adventures of a Blind Date -1.mp3"));  
        Chinesesorderlist.add ("He -10.mp3 The Adventures of Orderwrapper");  
        Chinesesorderlist.add (The new Orderwrapper ("I am the -11.mp3 of the Adventures of a Blind Date"));  
        Chinesesorderlist.add (The new Orderwrapper ("Ah, The Adventures of -12.mp3"));  
        Chinesesorderlist.add (The new Orderwrapper ("I am the -13.mp3 of the Adventures of a Blind Date"));  
        Chinesesorderlist.add (The new Orderwrapper ("I am the -25.mp3 of the Adventures of a Blind Date"));  
        Chinesesorderlist.add (The new Orderwrapper ("I am the -26.mp3 of the Adventures of a Blind Date")); ChinesesorDerlist.add (The new Orderwrapper ("I am the -2.mp3 of the Adventures of a Blind Date"));  
        Chinesesorderlist.add (The new Orderwrapper ("I am the -3.mp3 of the Adventures of a Blind Date"));  
        Chinesesorderlist.add (The new Orderwrapper ("I am the -4.mp3 of the Adventures of a Blind Date"));  
   
        Chinesesorderlist.add (The New Orderwrapper ("A" The Adventures of a Blind Date -4.mp3));  
        Collator Collatorchinese = collator.getinstance (Java.util.Locale.CHINA);  
       Collatorchinese = Collator.getinstance (Java.util.Locale.CHINESE);  
        Collections.sort (Chinesesorderlist, Collatorchinese);  

        Collections.sort (chinesesorderlist);  
        System.out.println ("Chinese + digit sort: =");  
            for (int i = 0; i &lt; chinesesorderlist.size (); i++) {Orderwrapper Chinese = chinesesorderlist.get (i);  
        System.out.println ("" + Chinese);
	} public static void Main (string[] args) {testnew (); }
}

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.