Sunday, February 23, 2014

java interface using the comparator ArrayList Sort invalid


posts by wre_most2 edited 2012-10-18 20:12:33
I want to mRelaItems sort (code below)
According ShortcutInfo the hotseatOrder property, hotseatOrder is an integer
private ArrayList mRelaItems = new ArrayList ();
...
MyComparator myComparator = new MyComparator ();
Collections.sort (mRelaItems, myComparator);

class MyComparator implements Comparator {

@ Override
public int compare (ShortCutInfo object1, ShortCutInfo object2) {
/ / TODO Auto-generated method stub
if (object1.hotseatOrder return 1;

return 0;
}

}

But mRelaItems no change after sorting Sort invalid
Please advise
Thank you! !

And I wrote it in accordance with descending or ascending order of implementation?<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
I debug your code a bit, no problem, you can achieve the sort, the result is descending, Here is the code I wrote
 import java.util.ArrayList; 
import java.util.Collections;
import java.util.Comparator;

public class Demo {

public static void main (String [] args) {
ArrayList mRelaItems = new ArrayList ();
mRelaItems.add (new ShortCutInfo (1));
mRelaItems.add (new ShortCutInfo (12));
mRelaItems.add (new ShortCutInfo (3));
mRelaItems.add (new ShortCutInfo (7));
mRelaItems.add (new ShortCutInfo (2));

MyComparator myComparator = new MyComparator ();
Collections.sort (mRelaItems, myComparator);
for (ShortCutInfo c: mRelaItems) {
System.out.println ("======" + c.hotseatOrder + "======");
}
}
}

class MyComparator implements Comparator {
@ Override
public int compare (ShortCutInfo object1, ShortCutInfo object2) {
if (object1.hotseatOrder return 1;

return 0;
}

}

Output:

====== 12 ======
====== 7 ======
====== 3 ======
====== 2 ======
====== 1 ======

Reply:
cited a floor reply:
I debug your code a bit, no problem, you can achieve the sort, the result is descending, the following is what I wrote code
Java code
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Demo {

public static void main (String [] ......

I still do not work here, ah

Reply:
reference to the second floor of the reply:
cited a floor reply:

I debug your code a bit, no problem, you can achieve the sort, the result is descending, Here is the code I wrote
Java code
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Demo {

public static v ......

Well, thank you!

Reply:
Support, refueling landlord
Reply:
How to solve ah
Reply:
Neighborhoods, my Comparator does not work ah ~ ~ do not know in the end is how children
 import java.util.ArrayList; 
import java.util.Collections;
/ **
* Test class
* /
public class Test {

public static void main (String [] args) {
ArrayList array = new ArrayList ();

DataEntity d1 = new DataEntity (1, 20.0);
DataEntity d2 = new DataEntity (2, 10.0);
DataEntity d3 = new DataEntity (3, 10.1);
DataEntity d4 = new DataEntity (4, 40);
DataEntity d5 = new DataEntity (5, 3.5);
array.add (d1);
array.add (d2);
array.add (d3);
array.add (d4);
array.add (d5);


SizeComparator cmp = new SizeComparator ();
Collections.sort (array, cmp);

for (DataEntity data: array) {
System.out.println (data);
}
}
}

import java.util.Comparator;

/ **
* Achieve Comparator interface class
* /
public class SizeComparator implements Comparator {

@ Override
public int compare (DataEntity arg0, DataEntity arg1) {
if (arg0.getSize () <= arg1.getSize ()) {
return 1;
}
return 0;
}

}

/ **
* Data entity classes
* /
public class DataEntity {
private int id;
private double size;

public DataEntity (int id, double size) {
this.id = id;
this.size = size;
}

@ Override
public String toString () {
return "id =" + id + "size =" + size;
}


public int getId () {
return id;
}

public void setId (int id) {
this.id = id;
}

public double getSize () {
return size;
}

public void setSize (double size) {
this.size = size;
}
}



The resulting output is simply not sorted:
id = 1 size = 20.0
id = 2 size = 10.0
id = 3 size = 10.1
id = 4 size = 40.0
id = 5 size = 3.5

Reply:
I found the problem. . . Hard to force ah. . That program can not run in jdk1.7 in, Comparator does not work, but replaced like a 1.6. . Stumped is the 1.7 bug,
Reply:
Comparator 1.7 in the return value must be one pair of opposite number, such as 1 and -1, 0 and 1 can not be, because the sorting algorithm 1.7 timsort, http://baike.baidu.com/link?url=UCowuf65GHz3cWVf_d7t0QzYUCcwU0QUwserNTIrImlaTBvBAaVfywzppQ70DqWKzUu3dPqsF21k9IDpT8QPE_

There are strict requirements for the return value

No comments:

Post a Comment