Java string Sorting Collections.sort () method

Source: Internet
Author: User

Http://hi.baidu.com/pjww/blog/item/01741d7fa563210a28388a95.html

Import java.io.*;
Import java.util.*;

public class Main {
public static void Main (String args[]) throws exception{
Scanner cin = new Scanner (system.in);
String str;
int n;
str = Cin.next ();
n = cin.nextint ();
Cin.nextline ();
list<string> myList = new arraylist<string> (n);
for (int i = 0; i < n; i++)
Mylist.add ((Cin.nextline ()). Trim ());
if (Str.compareto ("inc") = = 0)//Natural order
Collections.sort (MyList, New comparator<string> () {
public int Compare (string A, string b) {
return A.compareto (b);
}
});
if (Str.compareto ("dec") = = 0)//Natural reverse order (DEC)
Collections.sort (MyList, New comparator<string> () {
public int Compare (string A, string b) {
Return-a.compareto (b);
}
});
if (Str.compareto ("ncinc") = = 0)//Ignore Case order (Ncinc)
Collections.sort (MyList, New comparator<string> () {
public int Compare (string A, string b) {
return a.comparetoignorecase (b);
}
});
if (Str.compareto ("ncdec") = = 0)//Ignore case reverse order (NCDEC)
Collections.sort (MyList, New comparator<string> () {
public int Compare (string A, string b) {
Return-a.comparetoignorecase (b);
}
});
iterator<string> te = Mylist.iterator ();
while (Te.hasnext ()) {//Traverse output
System.out.println (Te.next ());
}
}
}

Http://blog.sina.com.cn/s/blog_5f17b8e40100g0g7.html

Package test.sort;

Import java.util.ArrayList;
Import Java.util.Comparator;
Import java.util.List;

public class Test {

public void Test1 () {
List<string> list=new arraylist<string> ();
List.add ("D");
List.add ("E");
List.add ("B");
List.add ("C");
List.add ("A");

System.out.println (list.get (0));
System.out.println (List.get (1));
System.out.println (List.get (2));
System.out.println (List.get (3));
System.out.println (List.get (4));
}

public static void Main (string[] args) {
Person P3=new person ("Careers3", 33);
Person P4=new person ("CAREERS4", 44);
Person P1=new person ("careers1", 11);
Person P2=new person ("careers2", 22);
List<person> list=new arraylist<person> ();
List.add (p3); List.add (p4); List.add (p1); List.add (p2);
System.out.println (List.get (0). Getage ());
System.out.println (List.get (1). Getage ());
System.out.println (List.get (2). Getage ());
System.out.println (List.get (3). Getage ());
System.out.println ("==================");

Java.util.Collections.sort (list,new Comparator () {
public int Compare (Object obj1,object obj2) {
Person a= (person) obj1;
Person b= (person) obj2;
int Age1=a.getage ();
int Age2=b.getage ();
if (age1==age2) {return 0;}
if (age1>age2) {return 1;}
return-1;
}
});
System.out.println (List.get (0). Getage ());
System.out.println (List.get (1). Getage ());
System.out.println (List.get (2). Getage ());
System.out.println (List.get (3). Getage ());
}
}
Class person{
private String name;
private int age;

Public person (String Name,int age) {
This.name=name;
This.age=age;
}

Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int age) {
This.age = age;
}
}

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.