-1

in this function my sort doesnot work correctly. my lastID is string

private void addDataToList(String LastID, String SmsBody) {
    List<ReceivedItemStructure> receivedItemStructures = 
      new ArrayList<ReceivedItemStructure>();
    ReceivedItemStructure rS = new ReceivedItemStructure();

    rS.setmLastID(LastID);
    rS.setmSmsBody(SmsBody);
    items.add(rS);

    Collections.sort(receivedItemStructures, new Comparator<ReceivedItemStructure>() {
        @Override
        public int compare(ReceivedItemStructure  rs1, ReceivedItemStructure  rs2)
        {
            if( rs1.getmLastID().equals(rs2.getmLastID()) )
                return 1;
            return 0;
        }
    });
    adapter.setRow(items);
    adapter.notifyDataSetChanged();
}

ReceivedItemStructure class:

public class ReceivedItemStructure {
    public String mLastID;
    public String mSmsBody;

    public ReceivedItemStructure(String vLastID, String vSmsBody) {
        mLastID  = vLastID;
        mSmsBody = vSmsBody;
    }

    public ReceivedItemStructure() {}

    public String getmLastID() {
        return mLastID;
    }

    public void setmLastID(String LastID) {
        mLastID = LastID;
    }

POST UPDATE catLog Result :

10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30390335
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30390100
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30389996
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30374135
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30374099
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30374093
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30374084
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30374062
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30373780
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30373756
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30390748
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30390755
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30391045
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30391076
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30391095
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30395978
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30395972
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30395990
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30396009
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30396045
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30396250
10-11 14:27:51.860    4658-4658/ir.tsms E/LastID:﹕ 30396259

getmLastID return string value and return rs1.getmLastID().compareTo(rs2.getmLastID()); not working. whats my code problem and how to resolve that?

Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
  • What does `getmLastID()` return? – Mureinik Oct 11 '14 at 14:16
  • @Mureinik i say in topic thats as an string and return String –  Oct 11 '14 at 14:20
  • So why isn't `return rs1.getmLastID().compareTo(rs2.getmLastID())` not working? Please share the input, the output you're getting and why you think the result is wrong. – Mureinik Oct 11 '14 at 14:24
  • possible duplicate of [Sorting a collection of objects](http://stackoverflow.com/questions/1206073/sorting-a-collection-of-objects) – Joe Oct 11 '14 at 14:28

1 Answers1

2

From the documentation of Comparator.compare() method (emphasis mine):

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is [respectively] less than, equal to, or greater than the second.

Is your compare function following the contract properly?


Unless the ID can be longer than 18 digits, I suggest that you store the ID as long type. Implementing the compare function will be as simple as:

public int compare(ReceivedItemStructure  rs1, ReceivedItemStructure  rs2)
{
    return rs1.getmLastID() - rs2.getmLastID();
}

If you still want to store your ID as String type:

public int compare(ReceivedItemStructure  rs1, ReceivedItemStructure  rs2)
{
    if (rs1.getmLastID().length() == rs2.getmLastID().length()) {
        return rs1.getmLastID().compareTo(rs2.getmLastID());
    } else {
        return rs1.getmLastID().length() - rs2.getmLastID().length();
    }
}

Using return rs1.getmLastID().compareTo(rs2.getmLastID()); directly will not work correctly when you compare 35 and 345, since String compares by lexicographic order and it does not correspond to the ordering of natural number when the numbers don't have the same number of digits.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • i return 1 or 0 with `if` method. thats not correct? –  Oct 11 '14 at 14:23
  • 1
    @andbee: Read the bold text. Is the ordering imposed by your code correct according to your expected result? – nhahtdh Oct 11 '14 at 14:24
  • @andbee: We don't know what your `ID` format is. We don't know if you want sort by String comparison or numeric comparison. So the best I can tell you is that: check your requirement, and write your function so that it follows the contract of `Comparator.compare` – nhahtdh Oct 11 '14 at 14:26
  • post updated. i'm paste result from logCat for LastID –  Oct 11 '14 at 14:30