项目中涉及到对大批量的数据进行打乱式排序,大概原理如下:
输入源数据:1,1,2,3,3,3,4,4
输出结果: 1,2,3,4,1,3,4,3
实现代码如下,采用递归的思想:
static <T> List<T> sort(List<T> numbers){ List<T> rights = Lists.newArrayList(); if(numbers.size == 0) { return rights; }else if(numbers.size() == 1){ rights.addAll(numbers); numbers.clear(); return rights; } List<T> lefts = Lists.newArrayList(); for(T num : numbers){ if(!rights.contains(num)){ rights.add(num); }else{ lefts.add(num); } } if(lefts.size > 0){ rights.addAll(sort(lefts)); lefts.clear(); } return rights; }