An example of the use of comparator

Source: Internet
Author: User

This two-day project needs to use a sort, corresponding to 4 items, so write a comparator class.

The code is as follows:

import Java.util.Comparator;

/**
* www.webkkk.net
* @author WEBKKK
*
*/
public class Testcomparator implements Comparator {

public int Compare (object arg0, Object arg1) {

string[] strf = ((String) arg0). Split ("_");
String[] Strl = ((String) arg1). Split ("_");
int len = strf.length;  
if (len = = 1) {
return Integer.parseint (strf[0])-Integer.parseint (strl[0]);

for (int i = 0; i < len; i++) {
if (!strf[i].equals (Strl[i])) {
return Integer.parseint (strf[i))-In Teger.parseint (Strl[i]);   
} else {
String strnf = Getsubvalue (strf, i);
String STRNL = Getsubvalue (Strl, i);
Return Compare (STRNF, STRNL);
}
}
return 0;
}  

public string Getsubvalue (string[] strobj, int npast) {
String strreturn = "";
 for (int i = 1 + npast i < strobj.length i++) {
Strreturn = Strreturn + "_" + strobj[i];

return strreturn.substring (1);br>}
}

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.