一、集合排序

数组排序:

int[] arr={1,2,3};

Arrays.sort(arr);

 

集合排序:使用Collections类中 sort()方法对List集合进行排序

 Collections.sort(list) 

根据元素的自然顺序对指定列表按升序进行排序

如果是字符串或者字符数据按照Ascall码值进行排序

 

二、集合排序案例

1、整型数据如何排序 

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class IntSort {

    public static void main(String[] args) {
        // 对存储在List中的整型数据进行排序
        List<Integer> list=new ArrayList<Integer>();
        list.add(5);
        list.add(9);
        list.add(3);
        list.add(1);
        System.out.println("排序前:");
        for(int n:list){
            System.out.print(n+"    ");
        }
        System.out.println();
        //对List中的数据进行排序
        Collections.sort(list);
        System.out.println("排序后:");
        for(int n:list){
            System.out.print(n+"    ");
        }

    }

}
IntSort

相关文章: