2012-06-26 48 views
1

我有一个public List<FriendProfile> friends = new ArrayList<FriendProfile>();。我通过从服务器读取信息初始化朋友列表。该FriendProfile对象包含一个INT称为private int userPosition;排序列表<>数值

一旦朋友列表已经被初始化,我想通过其在列表的索引0最高userPosition的FriendProfile对象,然后排序相应排序的好友列表,并且指数1次高userPosition ...

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

帮助表示赞赏!

+0

基于名称[对联系人进行排序的ArrayList的可能重复?](http://stackoverflow.com/questions/1814095/sorting-an-arraylist-of-contacts-based-on-name)和http://stackoverflow.com/questions/6957631/sort-java-collection – assylias

+0

[在列表视图中对数字进行排序](http://himanshugpt.wordpress.com/2010/09/10/sorting-list-in-java/) – Venky

回答

6

使用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<FriendProfile>

+0

谢谢 - 你可以举一个比较器的例子,它从高价值到低价值? –

+0

@LukeTaylor只是将“o1”换成“o2”成hmjd示例。像这样:public int compare(FriendProfile o2,FriendProfile o1) – hrules6872

0

使用Collections.Sort并编写自定义Comparator,根据userPosition进行比较。

+0

谢谢 - 你可以举一个例子和比较器,它从高价值到低价值? –

0

使用比较与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

+0

谢谢 - 你可以举一个例子和比较器,它从高价值到低价值? –

+0

不确定可以尝试:只需将返回-1更改为1并将其更改为-1 –

+0

http://stackoverflow.com/questions/1946668/sorting-using-comparator-descending-order-user-defined-classes –

1

实现可比接口。

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); 

列表将被排序。

0

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

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的静态方法。

0

You can usejava.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(); 

    } 

} 
1

现在没有必要拳击(即不需要使用新操作员使用创建OBJECT的valueOf insted的与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)对于Deascending顺序

Collections.sort(temp, new Comparator<XYZBean>() 
{ 
    @Override 
    public int compare(XYZBean lhs, XYZBean rhs) { 

     return Integer.valueOf(rhs.getDistance()).compareTo(lhs.getDistance()); 
     } 
}); 
+0

不适合我! –