我有数字例如593,我需要订购这些数字并按升序排列。我想知道的是如何按升序排列。这是我要订购的excel的几行。注意这些所有10,000行。
8/26/2012,Kristina,H,Chung,947 Martin Ave.,Muncie,CA,46489,khchung@business.com, $593
11/16/2012,Paige,H,Chen,15 MainWay Rd.,Dallas,HI,47281,phchen@business.com, $516
11/10/2012,Sherri,E,Melton,808 Washington Way,Brazil,CA,47880,semelton@business.com, $80
9/20/2012,Gretchen,I,Hill,56 Washington Dr.,Atlanta,FL,47215,gihill@business.com, $989
3/11/2012,Karen,U,Puckett,652 Maplewood Ct.,Brazil,FL,46627,kupuckett@business.com, $826
7/4/2012,Patrick,O,Song,679 MainWay Rd.,Lafayette,GA,47161,posong@business.com, $652
排序后,它看起来像这样
11/10/2012,Sherri,E,Melton,808 Washington Way,Brazil,CA,47880,semelton @ business.com,$ 80 11/16 / 2012,Paige,H,Chen,15 MainWay Rd。,Dallas,HI,47281,phchen @business.com,$ 516 8/26 / 2012,Kristina,H,Chung,947 Martin Ave.,Muncie,CA,46489,khchung @business.com,$ 593 7/4/2012,Patrick,O,Song,679 MainWay Rd。,Lafayette,GA,47161,posong @business.com,$ 652 3/11 / 2012,Karen,U,Puckett,652 Maplewood Ct。,Brazil,FL,46627,kupuckett @business.com,$ 826 9/20/2012,Gretchen,I,Hill,56 Washington Dr.,Atlanta,FL,47215,gihill @business.com,$ 989
但我需要知道如何编写代码才能执行此操作。
某些代码行: 字符串2; ArrayList persons = new ArrayList<>();
for(int i = 0; i < listOfCustomers.length; i++)
{
holder2 = listOfCustomers[i];
persons.setData(holder2);
}
Collections.sort(persons);
}
答案 0 :(得分:0)
以下是我的意思:
答案 1 :(得分:0)
创建一个arrayList:
ArrayList<Person> persons = new ArrayList<>();
创建一个只有一个简单字段的Person(在示例中称为data)。为文件中的每一行创建一个Person对象并调用setData。将它们添加到列表中。添加完所有对象后,在ArrayList上使用Collections.sort()。循环遍历ArrayList并在列表中的每个项目上调用getData。
public class Person implements Comparable<Person>{
private String data;
/**
* @return the data
*/
public String getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(String data) {
this.data = data;
}
@Override
public int compareTo(Person o) {
if (getValue(this) < getValue(o)) {
return -1;
} else if (getValue(this) > getValue(o)) {
return 1;
}
return 0;
}
private int getValue(Person p) {
String[] elements = p.getData().split(",");
String value = elements[elements.length - 1].trim();
if (value.startsWith("$")) {
value = value.substring(1);
}
return Integer.valueOf(value);
}
}
答案 2 :(得分:0)
String[] lines; //Assume initialized
for (int i = 0; i < lines.length; i++) {
String string = lines[i];
int number = Integer.valueOf(string.substring(string.lastIndexOf('$') + 1));
lines[i] = (char)number + string;
}
Arrays.sort(lines);
for (int i = 0; i < lines.length; i++) {
String string = lines[i];
lines[i] = string.substring(1);
}
for (String s : lines) {
System.out.println(s);
}
这在我的电脑上51ms排序了10710行。但请注意,只有当$ xxx小于Integer.MAX_VALUE
时,它才会起作用。