0

我用值填充了数组列表。每行都是具有属性的项目。现在我想按属性之一对项目进行排序并将它们“打印”到 textview。

ArrayList<String[]> arrayList = new ArrayList<String[]>();
final String[] rowToArray = new String[7];
rowToArray[0] = itemName;
rowToArray[1] = itemProperties1;
rowToArray[2] = itemProperties2;
rowToArray[3] = itemProperties3;
rowToArray[4] = itemProperties4;
rowToArray[5] = itemProperties5;
rowToArray[6] = itemProperties6;
arrayList.add(rowToArray);

您能否帮我按属性对其进行排序,然后向我展示如何使用属性逐个打印项目。

先感谢您。

编辑:

由 ppeterka66解决

我只需要添加他的代码并调用Collections.sort(arrayList,new StringArrayComparator(column)); 其中 column 是必须作为排序依据的列。

int i=0;
final int column=2;
Collections.sort(arrayList,new StringArrayComparator(column));
for(String[] line :arrayList) 
    {
    Log.d(Integer.toString(i),line[column].toString());
    }
4

2 回答 2

1
Collections.sort    

例如

class User {

    String name;
    String age;

    public User(String name, String age) {
        this.name = name;
        this.age = age;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
import java.util.Comparator;

public class ComparatorUser implements Comparator {

    public int compare(Object arg0, Object arg1) {
        User user0 = (User) arg0;
        User user1 = (User) arg1;

        int flag = user0.getAge().compareTo(user1.getAge());
        if (flag == 0) {
            return user0.getName().compareTo(user1.getName());
        } else {
            return flag;
        }
    }

}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortTest {

    public static void main(String[] args) {
        List userlist = new ArrayList();
        userlist.add(new User("dd", "4"));
        userlist.add(new User("aa", "1"));
        userlist.add(new User("ee", "5"));
        userlist.add(new User("bb", "2"));
        userlist.add(new User("ff", "5"));
        userlist.add(new User("cc", "3"));
        userlist.add(new User("gg", "6"));

        ComparatorUser comparator = new ComparatorUser();
        Collections.sort(userlist, comparator);

        for (int i = 0; i < userlist.size(); i++) {
            User user_temp = (User) userlist.get(i);
            System.out.println(user_temp.getAge() + "," + user_temp.getName());
        }

    }
}
于 2013-09-27T12:39:56.127 回答
1

您可以创建一个可重用的 String[] 比较器,您可以指定在哪些索引上比较数组:

public class StringArrayComparator implements Comparator<String[]> {
  //we store the index to compare the arrays by in this instance variable
  private final int stringIndexToCompare;

  //constructor accepting the value for the index to check
  public StringArrayComparator(int whichString) {
    stringIndexToCompare=whichString;
  }

  @Override
  public int compare(String[] o1, String[] o2) {
    //checking if any of the arrays is null
    if(o1==null)      { return o2==null?0:1; } //if o1 is null, o2 determines the resuult
    else if(o2==null) { return -1; } //this only gets evaluated if o1 is not null

    //get the strings, by checking if the arrays are long enough
    String first = o1.length>stringIndexToCompare?o1[stringIndexToCompare]:null;
    String second= o2.length>stringIndexToCompare?o2[stringIndexToCompare]:null;

    //null checking the strings themselves -- basically same as above
    if(first==null)          { return second==null?0:1; }
    else if(second==null)    { return -1; }

    //if both non-null, compare them.
    return first.compareTo(second);
  }
}  

要在您的列表中使用:

Collections.sort(myList,new StringArrayComparator(3));

注意:3 指定要比较的数组的索引。

您没有指定打印字符串的外观的预期输出,而只是为了打印列表,您可以使用这个 oneliner:

  System.out.println(Arrays.deepToString(a.toArray()));

编辑

我想看到类似 Log.d("line number",column[0]+","+column 1 +","+column[2]+...);

嘿,看起来几乎没问题......基本上你只需要将它放入一个循环中:这会逐行打印:

int lineNo=0;
for(String[] line :myList) {
    StringBuilder sb = new StringBuilder();
    sb.append(++i); //line number, incrementing too

    //iterating through the elements of the array
    for(int col=0;col<line.lenght;col++) {
        sb.append(",");
        if(line[col]!=null) {  //check for null....
            sb.append(line[col]);
        }
    }
    Log.d(sb.toString()); //append the value from the builder to the log.
}

要将它放在一个大字符串中:

int lineNo=0;
StringBuilder sb = new StringBuilder(); //create it here
for(String[] line :myList) {
    sb.append(++i); //line number, incrementing too

    //iterating through the elements of the array
    for(int col=0;col<line.lenght;col++) {
        sb.append(",");
        if(line[col]!=null) {  //check for null....
            sb.append(line[col]);
        }
    }
    sb.append("\n"); //append line break
}
Log.d(sb.toString()); //append the value from the builder to the log.

或者,为此目的使用 String.format() 可能会更好(虽然更慢),因为它提供了更好的格式:

 //assembly format string
 //if no line number was needed: String format = "";

 String format = "%d"; //line number, %d means integer
 for(int i=0;i<7;i++) {
    format+=",%20s"; //%20s means left aligned, 20 wide string
 }
 format += "\n"; //line break;

 int lineNumber=0;
 for(String[] line:myArray) {
     //if you didn't need the line number, it would be so easy here
     //String.format(format,line); //one line, but this doesn't have the line number yet...

     //with line numbers:
     int iamLazyNow = 0;
     String formatted = String.format(format,++lineNumber, 
          line[iamLazyNow++], line[iamLazyNow++],
          line[iamLazyNow++], line[iamLazyNow++],
          line[iamLazyNow++], line[iamLazyNow++],
          line[iamLazyNow++]); //practically one line, but ugly

     //you can append formatted to a StringBuilder, or print it here...
 }
于 2013-09-27T12:52:18.177 回答