java 字符串排序 Collections.sort() 方法

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 myList = new ArrayList(n);
      for ( int i = 0; i < n; i++ )
       myList.add((cin.nextLine()).trim());
      if ( str.compareTo("inc") == 0 )//自然顺序
       Collections.sort(myList, new Comparator() { 
        public int compare(String a, String b) { 
         return a.compareTo(b); 
         } 
        });
      if ( str.compareTo("dec") == 0 )//自然逆序(dec)
       Collections.sort(myList, new Comparator() { 
        public int compare(String a, String b) { 
         return -a.compareTo(b); 
         } 
        });
      if ( str.compareTo("ncinc") == 0 )//忽略大小写顺序(ncinc)
       Collections.sort(myList, new Comparator() { 
        public int compare(String a, String b) { 
         return a.compareToIgnoreCase(b); 
         } 
        });
      if ( str.compareTo("ncdec") == 0 )//忽略大小写逆序(ncdec)
       Collections.sort(myList, new Comparator() { 
        public int compare(String a, String b) { 
         return -a.compareToIgnoreCase(b); 
         } 
        });
      Iterator te = myList.iterator();
      while ( te.hasNext() ){//遍历输出
    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 list=new ArrayList();
        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 list=new ArrayList();
        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;
    }
}

 

你可能感兴趣的:(android)