user1386966
user1386966

Reputation: 3392

Sorting is wrong when comparing long values

I'm trying to sort long numbers in ASC but seems that the comparison is wrong. There is a sequence of correct digits, but from the 7th digit it all messes up. Can anyone advise why?

The classes:

public class MyTime {

    private long timeInMicroSeconds;

    public MyTime (long timeInMicroSeconds) {
        this.timeInMicroSeconds = timeInMicroSeconds;
    }
}

public class tester implements Comparator<MyTime> {

      public int compare(MyTime o1, MyTime o2) {
          return (int) ( (-1) * (o2.getTimeInMicroSeconds() - o1.getTimeInMicroSeconds()));
}

}

This is the main test with my numbers :

  MyTime t1 = new MyTime (1482072568710018L);
    MyTime  t2 = new MyTime (1482068966855246L);
    MyTime t3 = new MyTime (1482068967752058L);
    MyTime t4 = new MyTime (1482069164096129L);
    MyTime t5 = new MyTime (1482072704590983L);
    MyTime t6 = new MyTime (1482068963206124L);
    MyTime t7 = new MyTime (1482069164097807L);
    MyTime t8 = new MyTime (1482068962786004L);
    MyTime t9 = new MyTime (1482069034105390L);
    MyTime t10 = new MyTime (1482068979718112L);
    MyTime t11 = new MyTime (1482068963143736L);
    MyTime t12 = new MyTime (1482069164098280L);
    MyTime t13 = new MyTime (1482069029615872L);
    MyTime t14 = new MyTime (1482072704590408L);



    List<MyTime > n = new ArrayList<MyTime >();
    n.add(t1);
    n.add(t2);
    n.add(t3);
    n.add(t7);
    n.add(t11);
    n.add(t14);
    n.add(t10);
    n.add(t9);
    n.add(t6);
    n.add(t2);
    n.add(t4);
    n.add(t12);
    n.add(t13);
    n.add(t5);
    n.add(t8);



 //RUNNING THE SORT
   System.out.println("printing before : " );
    for(int i = 0 ; i < n.size() ; i ++)
    {
        System.out.println(n.get(i).getTimeInMicroSeconds());
    }

    Collections.sort(n, new tester());
    System.out.println("printing after : " );
    for(int i = 0 ; i < n.size() ; i ++)
    {
        System.out.println(n.get(i).getTimeInMicroSeconds());
    }

And this is the output:

    printing before : 
1482072568710018
1482068966855246
1482068967752058
1482069164097807
1482068963143736
1482072704590408
1482068979718112
1482069034105390
1482068963206124
1482068966855246
1482069164096129
1482069164098280
1482069029615872
1482072704590983
1482068962786004


printing after : 
1482072568710018
1482072704590408
1482072704590983
1482068962786004
1482068963143736
1482068963206124
1482068966855246
1482068966855246
1482068967752058
1482068979718112
1482069029615872
1482069034105390
1482069164096129
1482069164097807
1482069164098280

You can see that : 1482072568710018 < 1482072704590408 < 1482072704590983 > 1482068962786004

any advise what I did wrong?

Upvotes: 1

Views: 1086

Answers (2)

Timothy Truckle
Timothy Truckle

Reputation: 15622

The problem is the cast in

 return (int) ( (-1) * // ...

at that point you just trunk the upper 4 bytes from the long value getting a random bit being the sign indicator.

Therefore you get a random number.

As the other already stated you'd better use

Long.compare()

and if you have reverse the sort just switch the position of the compared numbers instead of doing * -1.

Upvotes: 4

GhostCat
GhostCat

Reputation: 140525

Why implement your own comparison here? There is already a "default" way to compare to long values, and that is Long.compare(). So simply change your method to call that static method:

public int compareTo(MyTime ...) {
  return Long.compare(o1.get...
}

Upvotes: 4

Related Questions