美文网首页
List> excel根据某一列排序

List> excel根据某一列排序

作者: 小铭铭_7c47 | 来源:发表于2019-01-11 18:53 被阅读0次
package hehaoming.common;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class test {
    
    public static void main(String[] args) throws Exception{
        List<List> ll = new ArrayList();
        List<String> lll1 = new ArrayList<String>();
        List<String> lll2 = new ArrayList<String>();
        List<String> lll3 = new ArrayList<String>();
        List<String> lll4 = new ArrayList<String>();
        lll1.add("1A");
        lll1.add("1B");
        lll1.add("1C");
        
        lll2.add("2A");
        lll2.add("2B");
        lll2.add("2C");
        
        lll3.add("3A");
        lll3.add("3B");
        lll3.add("3C");
        
        lll4.add("4A");
        lll4.add("4B");
        lll4.add("4C");     
        
        ll.add(lll1);
        ll.add(lll3);
        ll.add(lll4);
        ll.add(lll2);
        System.out.println("排序前:");     
        for(List l : ll){
            System.out.println(l);
        }
        sortList(ll, 1);
        
        System.out.println("排序后:"); 
        for(List l : ll){
            System.out.println(l);
        }

//    输出:       
//      排序前:
//      [1A, 1B, 1C]
//      [3A, 3B, 3C]
//      [4A, 4B, 4C]
//      [2A, 2B, 2C]
//      排序后:
//      [1A, 1B, 1C]
//      [2A, 2B, 2C]
//      [3A, 3B, 3C]
//      [4A, 4B, 4C]
                
    }
    
    static void sortList (List<List> data, int columnNum){
        Collections.sort(data,new Comparator<List>(){
            public int compare(List o1, List o2){
                String a = (String) o1.get(columnNum);
                String b = (String) o2.get(columnNum);
                return a.compareTo(b);
            }
        });
    }
}

参考自:
https://my.oschina.net/liuyuanyuangogo/blog/151628

相关文章

网友评论

      本文标题:List> excel根据某一列排序

      本文链接:https://www.haomeiwen.com/subject/wvcfdqtx.html