2014-02-20 17 views
-1

如何使此代码降序..?如何使此代码降序

for (int x = 0; x < a.length; x++) { 
    for (int y = x + 1; y < a.length; y++) { 
    String Name1 = a[y].getBrand(); 
    String Name2 = a[x].getBrand(); 
    if (Name1.compareTo(Name2) <= 0) { 
     Get temp = a[x]; 
     a[x] = a[y]; 
     a[y] = temp; 
    } 
    } 
} 
+1

网络充满了排序算法。找到适合您需求的产品。 –

+0

感谢它的作品:) – user3201390

回答

4

在comparitation只要改变这样的:

if (Name1.compareTo(Name2) >= 0) { 
+1

“比较”;) – fge

+0

@fge:谢谢。 :) –

+0

感谢它的作品:) – user3201390

1

不要做,使用Arrays.sort()

private static final Comparator<Get> DESC = new Comparator<Get>() 
{ 
    @Override 
    public int compare(final Get a, final Get b) 
    { 
     return b.getBrand().compareTo(a.getBrand()); 
    } 
} 


// In code... 
Arrays.sort(theArray, DESC); 
1

你为什么不使用Arrays.sort() 这会让你的问题失控。