---
title: "Sorting ArrayList Based on Value of Long using java"  
description: "Sorting ArrayList Based on Value of Long using java"  
author: "Anonymous User"  
published: 2013-10-05  
updated: 2013-10-05  
canonical: https://www.mindstick.com/forum/1611/sorting-arraylist-based-on-value-of-long-using-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Sorting ArrayList Based on Value of Long using java

I am attempting to sort an [ArrayList](https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp) based on the [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) of a long [present](https://answers.mindstick.com/qa/96635/explain-about-the-various-features-present-in-ms-access) within each object. After following various examples around the [internet](https://www.mindstick.com/articles/44650/what-should-you-do-during-an-internet-outage), I have come up with the following code but it is not [sorting](https://www.mindstick.com/articles/1581/sorting-list-with-side-bar) as desired (it seems to [truncate](https://www.mindstick.com/blog/313/difference-between-truncate-delete-statement-in-sql-server) parts of the object).

```
public static Comparator<Customer> compareSIN =
         new Comparator<Customer>() {
            public int compare(Customer cust1, Customer other) {
               String sin1 = "" + cust1.sin;
               String sin2 = "" + other.sin;
               return sin1.compareTo(sin2);
            }
         };
```

Please [advise me](https://answers.mindstick.com/qa/37419/what-games-on-android-would-you-advise-me-to-play) on what I am doing [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on) in the first snippet of code that is [preventing](https://www.mindstick.com/news/2244/issue-preventing-users-from-accessing-facebook-s-social-networking-platforms-has-been-resolved) me from sorting the [objects](https://www.mindstick.com/forum/145447/what-are-objects) properly.\

Thanks!

## Replies

### Reply by Anonymous User

You do not actually need to use a compareTo() method inside your own compareTo() method.

The compare to states that it must return 0 if they are equal and negative or positive numbers for non equality.

For this reason you can compare two longs by returning the one subtracted from the other.

```
public int compare(Customer cust1, Customer other) {
           return cust1.sin - other.sin;
}
```


---

Original Source: https://www.mindstick.com/forum/1611/sorting-arraylist-based-on-value-of-long-using-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
