Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8720051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:56:33+00:00 2026-06-13T06:56:33+00:00

I am attempting to sort an ArrayList based on the value of a long

  • 0

I am attempting to sort an ArrayList based on the value of a long present within each object. After following various examples around the internet, I have come up with the following code but it is not sorting as desired (it seems to truncate 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 on what I am doing missing in the first snippet of code that is preventing me from sorting the objects properly.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:56:33+00:00Added an answer on June 13, 2026 at 6:56 am

    From the title I assume Customer.sin is a long – and the problem is you are trying to compare them as Strings rather then by their numeric value.

    (Example: 10000 is lexicographically smaller then 2 – so using Strings here is the fault)

    You should use Long.compare() (Assuming java 7):

    public static Comparator<Customer> compareSIN = 
             new Comparator<Customer>() {
                public int compare(Customer cust1, Customer other) {
                   return Long.compare(cust1.sin,other.sin);
                }
             };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting sort this array by each stdClass Object's [title] in an Ubercart
I'm attempting to sort of highlight a tile object within a game I'm making
I am attempting to sort an ArrayList of TokenDoubleCounters (my custom object). In TokenDoubleCounter,
I'm attempting to SORT an ArrayList by (Type Date) MyObject.getDate() My format is formatted
I'm attempting to sort this JSON object: JSONObject = { command: [{ geobox: [...],
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
I'm attempting to do a simple bubble sort code to get familiar with list/string
I have sort of a tricky problem I'm attempting to solve. First of all,
I am attempting to implement Heap sort in my program to learn more about
Possible Duplicate: C++ template, linking error I am attempting to implement a selection sort,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.