【问题标题】:Sorting List<> by numeric value按数值排序列表<>
【发布时间】:2012-06-27 17:18:52
【问题描述】:

我有一个public List&lt;FriendProfile&gt; friends = new ArrayList&lt;FriendProfile&gt;();。我通过从服务器读取信息来初始化好友列表。 FriendProfile 对象包含一个名为 private int userPosition; 的 int

一旦朋友列表被初始化,我想通过在列表的索引 0 处具有最高 userPosition 的 FriendProfile 对象对朋友列表进行排序,然后相应地排序,索引 1 与第二高 @987654327 @...

我想我可以写一个排序算法,但我正在寻找预先编写的代码(也许 JDK 有一些方法可以提供?)

感谢您的帮助!

【问题讨论】:

标签: java android list sorting


【解决方案1】:

使用Collections.sort() 并指定Comparator

Collections.sort(friends,
                 new Comparator<FriendProfile>()
                 {
                     public int compare(FriendProfile o1,
                                        FriendProfile o2)
                     {
                         if (o1.getUserPosition() ==
                                 o2.getUserPosition())
                         {
                             return 0;
                         }
                         else if (o1.getUserPosition() <
                                      o2.getUserPosition())
                         {
                             return -1;
                         }
                         return 1;
                     }
                 });

或者让FriendProfile实现Comparable&lt;FriendProfile&gt;

【讨论】:

  • 谢谢 - 你能举一个比较器的例子吗?它把高价值排序到低价值?
  • @LukeTaylor 只需将“o1”转换为“o2”到 hmjd 示例中。像这样: public int compare(FriendProfile o2, FriendProfile o1)
  • 更简单。只需返回 getPosition() - o.getPosition();
【解决方案2】:

实现可比接口。

class FriendProfile implements Comparable<FriendProfile> {

    private int userPosition;

    @Override
    public int compareTo(FriendProfile o) {

        if(this.userPosition > o.userPosition){
            return 1;
        }
        return 0;
    }

}

只需调用 Collection.sort(List) 方法即可。

    FriendProfile f1=new  FriendProfile();
    f1.userPosition=1;
    FriendProfile f2=new  FriendProfile();
    f2.userPosition=2;
    List<FriendProfile> list=new ArrayList<FriendProfile>();
    list.add(f2);
    list.add(f1);
    Collections.sort(list);

列表将被排序。

【讨论】:

    【解决方案3】:

    现在无需装箱(即无需使用新运算符创建 OBJECT 使用 valueOf 与 Collections.Sort 的 compareTo..)

    1)升序

    Collections.sort(temp, new Comparator<XYZBean>() 
    {
         @Override
         public int compare(XYZBean lhs, XYZBean rhs) {
    
           return Integer.valueOf(lhs.getDistance()).compareTo(rhs.getDistance());
          }
     });
    

    1)对于降序

    Collections.sort(temp, new Comparator<XYZBean>() 
    {
         @Override
         public int compare(XYZBean lhs, XYZBean rhs) {
    
           return Integer.valueOf(rhs.getDistance()).compareTo(lhs.getDistance());
          }
     });
    

    【讨论】:

      【解决方案4】:

      使用Collections.Sort 并编写一个自定义的Comparator,它基于userPosition 进行比较。

      【讨论】:

      • 谢谢 - 你能举一个比较器的例子吗?它把高价值排序到低价值?
      【解决方案5】:

      将 Comparator 与 Collections.sort 方法一起使用

      java.util.Collections.sort(list, new Comparator<FriendProfile >(){
           public int compare(FriendProfile a,  FriendProfile b){
                if(a.getUserPosition() > b.getUserPosition()){
                   return 1;
                 }else if(a.getUserPosition() > b.getUserPosition()){
                  return -1;
               }
                return 0;
           }
      });
      

      看到这个link

      【讨论】:

      【解决方案6】:

      有两种方法可以做到这一点。

      1。 FriendProfile 可以实现 Comparable 接口。

      public class FriendProfile implements Comparable<FriendProfile>
      {
         public int compareTo(FriendProfile that)
         {
           // Descending order
           return that.userPosition - this.userPosition;
         }
      }
      
      ...
      
      Collections.sort(friendProfiles);
      

      2。 你可以写一个比较器。

      public class FriendProfileComparator implements Comparator<FriendProfile>
      {
         public int compare(FriendProfile fp1, FriendProfile fp2) 
         {
           // Descending order
           return fp2.userPosition - fp1.userPosition;
         }
      }
      
      ...
      
      Collections.sort(friendProfiles, new FriendProfileComparator());
      

      当比较对象而不是基元时,请注意您可以委托给包装对象 compareTo。例如return fp2.userPosition.compareTo(fp1.userPosition)

      如果对象具有您想要实现的自然顺序,第一个非常有用。比如 Integer 实现了数字顺序,String 实现了字母顺序。如果您在不同情况下需要不同的订单,则第二个很有用。

      如果你写了一个比较器,那么你需要考虑把它放在哪里。由于它没有状态,您可以将其编写为 Singleton 或 FriendProfile 的静态方法。

      【讨论】:

        【解决方案7】:

        You can use java.lang.Comparable 接口,如果你只想以一种方式排序

        But if you want to sort 不止一种方式,使用 java.util.Compartor 接口。

        例如:

        对象将按其 roll_nos 排序的类

        public class Timet {
        
            String name;
            int roll_no;
        
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public int getN() {
                return roll_no;
            }
            public void setN(int n) {
                this.roll_no = n;
            }
            public Timet(String name, int n) {
        
                this.name = name;
                this.roll_no = n;
            }
        
            public String toString(){
                return this.getName();
        
        
        
            }
        
        }
        

        排序类:

        public class SortClass {
        
        
            public void go(){
        
                ArrayList<Timet> arr = new ArrayList<Timet>();
                arr.add(new Timet("vivek",5));
                arr.add(new Timet("alexander",2));
                arr.add(new Timet("catherine",15));
        
                System.out.println("Before Sorting :"+arr);
        
        
        
        
        
                Collections.sort(arr,new SortImp());
        
                System.out.println("After Sorting :"+arr);
        
        
            }
            class SortImp implements Comparator<Timet>{
        
                @Override
                public int compare(Timet t1, Timet t2) {
        
        
        
        
                    return new Integer(t1.getN()).compareTo (new Integer((t2.getN())));
                }
        
        
        
            }
            public static void main(String[] args){
        
                SortClass s = new SortClass();
                s.go();
        
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-10-26
          • 2021-01-24
          • 2015-01-11
          • 2018-11-03
          • 2014-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多