使用 Comparator 比较 List数据大小

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView listview = (ListView)this.findViewById(R.id.listview);
       String[] strs = new String[]{"2","0","1","dfdf","Uifd","太阳","oppopo","dfjdkfjd","ADFDFDFI","今天"};        
         List aList =  Arrays.asList(strs);
        
        StrCompare strCompare = new StrCompare();
        Collections.sort(aList, strCompare);
        
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, strs);
        listview.setAdapter(adapter);
     }
    
    public class StrCompare implements Comparator{

	public int compare(String object1, String object2) {
			//默认升序排列
			return object1.compareTo(object2);
			
	}
    	
    }
 

 

你可能感兴趣的:(android)