java 如何在java-me中对字符串向量进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6569414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort a Vector of String in java-me?
提问by cdegroot
I have a Vector of Integer ( primary key of a database table ) ; and I implemented a method which returns a String based on this primary key Integer. My problem is that I want to put these String's into a Vector and they are "sorted" in the Vector. How to achieve this String sort ?
我有一个整数向量(数据库表的主键);我实现了一个方法,该方法基于这个主键整数返回一个字符串。我的问题是我想将这些字符串放入向量中,并且它们在向量中“排序”。如何实现这种字符串排序?
回答by bharath
Use the following code for sort the vector in java-me.
使用以下代码对 java-me 中的向量进行排序。
public Vector sort(Vector sort) {
Vector v = new Vector();
for(int count = 0; count < e.length; count++) {
String s = sort.elementAt(count).toString();
int i = 0;
for (i = 0; i < v.size(); i++) {
int c = s.compareTo((String) v.elementAt(i));
if (c < 0) {
v.insertElementAt(s, i);
break;
} else if (c == 0) {
break;
}
}
if (i >= v.size()) {
v.addElement(s);
}
}
return v;
}
回答by duffymo
You can either use a TreeSet
, which will keep Strings in sorted order, or use something like this before you return them:
您可以使用 a TreeSet
,它将按排序顺序保存字符串,或者在返回它们之前使用类似的东西:
Collections.sort(yourVector);
Another alternative is to ask the database to ORDER BY primary key. Let the database do the work.
另一种选择是要求数据库按主键 ORDER BY。让数据库来做这项工作。
Your query is probably returning more than the primary keys. I'd wonder why you're dealing with things on the primitive level of a String
and primary key. I'll bet you're not thinking enough in terms of objects. Where there's a primary key, other data should be following close behind. I'd encapsulate all of them together and worry about sorting thoseobjects.
您的查询可能返回的不仅仅是主键。我想知道你为什么要在 aString
和主键的原始级别上处理事情。我敢打赌,您在对象方面考虑得还不够多。在有主键的地方,其他数据应该紧随其后。我会将它们全部封装在一起,并担心对这些对象进行排序。
Why Vector
? I'd prefer ArrayList
, because it's not synchronized by default. Better performance.
为什么Vector
?我更喜欢ArrayList
,因为默认情况下它不同步。更好的性能。
回答by npinti
You will need to implement your own comparator by implementing the Comparatorinterface.
您需要通过实现Comparator接口来实现自己的比较器。
Something like this should do the trick:
像这样的事情应该可以解决问题:
Collections.sort(vect, new Comparator() {
public int compare(Object a, Object b) {
return ( new Integer(((MyClass) a).getNumber()) ).compareTo( new Integer(((MyClass) b).getNumber()));
}
});
Taken from here.
取自这里。
回答by adamjmarkham
Pass the vector to the static method Collections.sort(vector)
not sure how you want it sorted but this method will sort according to the natural order of the objects contained in the vector, given by the compareTo
method of each object.
将向量传递给静态方法,Collections.sort(vector)
不确定您希望它如何排序,但此方法将根据向量中包含的对象的自然顺序进行排序,由compareTo
每个对象的方法给出。
The Collections APImay be able to help you out further.
该集合API可能能够进一步帮助你。
回答by cdegroot
If it's Java, are you looking for
如果是 Java,您是否正在寻找
Collections.sort(vector);
?
?
回答by Peter Lawrey
Vector is slower than ArrayList as its a thread safe collection.
Vector 比 ArrayList 慢,因为它是线程安全的集合。
However the sort operation is not thread safe. As such to sort a vector in a thread safe manner you need to synchronize it.
然而,排序操作不是线程安全的。因此,要以线程安全的方式对向量进行排序,您需要对其进行同步。
synchronized(vector) {
Collections.sort(vector);
}
If you don't need the collection to be thread safe, consider using an ArrayList.
如果您不需要集合是线程安全的,请考虑使用 ArrayList。
回答by Rupok
import java.util.Vector;
import java.util.Collections;
public class SortJavaVectorExample {
public static void main(String[] args) {
//create Vector object
Vector v = new Vector();
//Add elements to Vector
v.add("1");
v.add("3");
v.add("5");
v.add("2");
v.add("4");
/*
To sort a Vector object, use Collection.sort method. This is a
static method. It sorts an Vector object's elements into ascending order.
*/
Collections.sort(v);
//display elements of Vector
System.out.println("Vector elements after sorting in ascending order : ");
for(int i=0; i<v.size(); i++)
System.out.println(v.get(i));
}
}
回答by MOHAMMAD S HUSSAIN
You can either use a Collections class, which will keep Strings in sorted order, or use something like this before you return them:
您可以使用 Collections 类,它将按排序顺序保存字符串,或者在返回它们之前使用类似的东西:
if you want to sort it in ascending order use only
如果您想按升序对其进行排序,请仅使用
Collections.sort(put your....LIST/ARRAYLIST/VECTOR OBJECT HERE);
Collections.sort(把你的....LIST/ARRAYLIST/VECTOR OBJECT放在这里);
if you want to sort it in descending order use only
如果您想按降序对其进行排序,请仅使用
** Collections.sort(put your....LIST/ARRAYLIST/VECTOR OBJECT HERE);** ** Collections.reverse(put your....LIST/ARRAYLIST/VECTOR OBJECT HERE);**
** Collections.sort(把你的....LIST/ARRAYLIST/VECTOR OBJECT放在这里);** ** Collections.reverse(把你的....LIST/ARRAYLIST/VECTOR OBJECT放在这里);**