如何对对象的arylylist进行排序?

时间:2014-04-13 06:08:53

标签: java sorting arraylist

我有一个班级

public class SMS 
{
    public String addr;
    public String body;
    public String type;
    public String timestamp;
}

现在我已经创建了一个对象的数组列表

ArrayList<SMS> temp = new ArrayList<SMS>();

我添加了值。现在我想根据时间戳对arrayList进行排序。

如何根据时间戳以升序/降序对arrayList进行排序?

4 个答案:

答案 0 :(得分:7)

Collections.sort(temp);

将对Comparable个对象进行排序,因此类SMS必须实现Comparable<SMS>

public class SMS implementes Comparable<SMS>
{
    public String addr;
    public String body;
    public String type;
    public String timestamp;

    @Override
    public int compareTo(SMS other) {
       //for instance
       return addr.compareTo( other.addr );
    }
}

在实施Comparable同时实现equals()hashcode()时,通常这是一种很好的做法,因此对象之间的相等性与它们的比较一致。此外,您应该添加一些针对空值的保护:

public class SMS implements Comparable<SMS>
{
    public String addr;
    public String body;
    public String type;
    public String timestamp;

    @Override
    public int compareTo(SMS other) {
       //for instance
       if( addr == null ) {
           return Integer.MAX_VALUE;
       }
       return addr.compareTo( other.addr );
    }

    @Override
    public boolean equals(Object other) {
       //for instance
       if( other instanceof SMS ) {
          if( addr == null && ((SMS) other) != null ) {
              return false;
          }
          return addr.equals(((SMS) other).addr);
       } else {
          return false;
       }
       return addr.compareTo( other.addr );
    }

    @Override
    public int hashcode() {
       //for instance
       if( addr == null ) {
           return 0;
       }
       return addr.hashcode();
    }
}

答案 1 :(得分:5)

要比较包含时间戳的字符串,您需要首先将它们解析为长Long.parseLong(timestamp),然后使用Long.compare(x,y)比较数值。

所以尝试使用Collections.sort(yorList, yourOwnComparator)喜欢

Collections.sort(temp, new Comparator<SMS>() {
    @Override
    public int compare(SMS o1, SMS o2) {
        return Long.compare(Long.parseLong(o1.timestamp), 
                            Long.parseLong(o2.timestamp));
    }
});

如果您可以将时间戳类型更改为long,则此代码可能看起来像

Collections.sort(temp, new Comparator<SMS>() {
    @Override
    public int compare(SMS o1, SMS o2) {
        return Long.compare(o1.timestamp, o2.timestamp);
    }
});

在Java8中,您甚至可以使用lambdas来进一步缩短代码

Collections.sort(temp, (SMS o1, SMS o2) -> Long.compare(o1.timestamp, o2.timestamp));

甚至

Collections.sort(temp, (o1,  o2) -> Long.compare(o1.timestamp, o2.timestamp));

答案 2 :(得分:0)

使用Comparable(参见example here), 它应该是这样的:

public class SMS implements Comparable<SMS>{

    ....

    public int compareTo(SMS compareSms) { 
        return this.timestamp.compareTo(compareSms.timestamp);
    }
}

答案 3 :(得分:0)

这是一般测试代码:

ArrayList<SMS> temp = new ArrayList<SMS>();

// testing values
SMS a = new SMS();
a.setTime("a time");
SMS b = new SMS();
b.setTime("b time");
SMS c = new SMS();
c.setTime("c time");
temp.add(a);
temp.add(b);
temp.add(c);

// descending sort
Collections.sort(temp, new Comparator<SMS>() {
    public int compare(SMS text1, SMS text2) {
        // compare timestamps from the SMSs
        return text2.getTime().compareTo(text1.getTime());
    }
});

// print it out to test
for (SMS sms : temp) {
    System.out.println(sms.getTime());
}

如果我想将其从升序更改为降序,我只需要切换比较文本消息的方式。看看这个:

return text2.getTime().compareTo(text1.getTime()); // descending
return text1.getTime().compareTo(text2.getTime()); // ascending

注意:我假设getTime()会返回短信的时间戳,您需要将此导入语句添加到您的班级:import java.util.Collections;import java.util.*;

要使用getTime()setTime(String timestamp)方法,您的课程可能如下所示:

public class SMS 
{
    public String addr;
    public String body;
    public String type;
    public String timestamp;

    public void setTime(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getTime() {
        return timestamp;
    }
}