代码改变世界

Collections的排序之一(Java)

2016-11-18 18:38  甘雨路  阅读(155)  评论(0编辑  收藏  举报
package home.collection.arr;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.omg.CORBA.PUBLIC_MEMBER;

public class HomeColletion {
    public static void main(String[] args) {
        
        // 创建List
        List<Integer> numlist = new ArrayList<Integer>();
        
        int count = 0;
        Scanner scanner = new Scanner(System.in);
        // 输入10个数,存到List
        do {
            System.out.println("请输入第"+(count+1)+"个数:");
            int number = scanner.nextInt();
            numlist.add(number);
            count++;
        } while (count<10);

        //排序    
        Collections.sort(numlist,new Comparator<Integer>(){
            public int compare(Integer num1,Integer num2){
                return num2.hashCode()-num1.hashCode();
            }
        });
        System.out.println(numlist);
        
        
    }
        
}