2012-05-09 51 views
0

我创建了一个Vector对象来存储Table对象中的数据,如Vector<Table>Vector<Table>包含如下组件。向量排序JAVA

[Vector<Record> records, String tableName, String keyColumnName, int recordCount, int columnCount] 

我需要在上述载体排序tableName我自己的订单,并与sorted tableNames返回Vector<Table>其他进程。

我怎么能这样做?

修订

我有如下写道方法。

private Vector<Table> orderTables(Vector<Table> loadTables) { 

    ArrayList<String> tableNames = new ArrayList<String>(); 

    for (Table table : loadTables) { 

     String tblName = table.getTableName(); 
     tableNames.add(tblName); 

    } 
    Collections.sort(tableNames, new MyComparable()); 

    return null; 
} 

但我不知道怎么写Comparator这个想法。我自己的排序顺序存储在.properties文件中。我可以阅读它并获得价值。但我不知道如何比较它。

我该怎么办?

回答

8

我建议您使用Collections.sort与您自己的自定义Comparator

+0

而另一个建议:避免使用'VECTOR'。使用任何其他'List'实现('ArrayList','LinkedList'等)供您选择。 – AlexR

+0

@AlexR,@aioobe:我更新了我的问题。我不知道如何编写“比较器”。我研究了一些从谷歌搜索中找到的例子。但我无法为我的必要写一个“比较器”。 – Bishan

0

只是举一个伪

TableComparator implements Comparator{ 
    int compare(Table source, Table destination){ 
    //implement your own custom logic of comparison 
    } 
} 

Vector<Table> vectorTable = new Vector<Table>(); 
TableComparator tableComparator = new TableComparator(); 

现在用这个比较中Collections.sort(vectorTable,tableComparator);

+0

@ raddykrish:无法在'Collections.sort()'中使用Vector。它应该是'Collections.sort(List list,Comparator c)' – Bishan

+0

Vector也实现List。检查http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Vector.html – raddykrish