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

  • SEARCH
  • Home
  • 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 4100230
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:32:22+00:00 2026-05-20T20:32:22+00:00

How can i sort ArrayList in java without using comparator.? I have already used

  • 0

How can i sort ArrayList in java without using comparator.?

I have already used comparator and it work not properly for me, means data is not in particuler manner.

I have one array list that contain following properties…

getTotal(),
getID(),
getRank(),
getItemName(),
getFinalRank()

I have sotred this all this into one arraylist itemWiseDetails

now i want to make poi report and display all this details but according to rank of that
itemName. ANd one more thing my rank is in String so when i Tried to sort based on this rank it take N/A data as a 0 rank so it displayed first then it display first rank , then second and go on ..

So, I want to sort this itemWiseDetails list without comparator

Thanks in advance,

I have implement Comparator like this way

 public int compareTo(Object itemDetailVO) 
    {
    if (!(itemDetailVOinstanceof ItemDetailVO)) throw new ClassCastException("AItemDetailVOobject expected."); 
    int otherItemRank = Integer.parseInt(((ItemDetailVO) itemDetailVO).getRank().toString()); 
return Integer.parseInt(this.trLotRank.toString())- otherBidderRank;
    }
  • 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-05-20T20:32:23+00:00Added an answer on May 20, 2026 at 8:32 pm

    You have two options:

    • Make your class implement Comparable, and call Collections.sort without specifying a comparator
    • Implement the comparator properly

    You seem to be taking the approach of “I tried X and it didn’t work, therefore I need to try something else” – but the reason X (using a comparator) didn’t work appears to be that your comparator had bugs… it didn’t compare items in the way that you wanted it to. You’ll run into exactly the same problem when you implement Comparable. You’ve still got the same fundamental work to do (working out how to compare two items) – it’s really just a matter of where that logic goes.

    Generally implementing a Comparator is a more flexible approach, in that it allows a collection to be sorted in different ways depending on your needs. It also allows you to sort collections where you can’t change the code within the element class itself. I would personally stick to the Comparator approach.

    Write some unit tests comparing various items, and then implement the comparator so that those tests pass. Then if you see any more problems, add a test for that case and make that pass too. Iterate until your comparator is working properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JSF and I have a complex datatable where the user can sort
I have a little dilemma that maybe you can help me sort out. I've
I have to sort a number of integers, which can have values between 30.000.000
I know that JTable can sort by a single column. But is it possible
It's helpful to name threads so one can sort out which threads are doing
How can I display a sort arrow in the header of the sorted column
Can anyone tell the function to sort the columns of a gridview in c#
I can't get my GridView to enable a user to sort a column of
I'd sort of like to use SQLite from within C#.Net, but I can't seem
You can pass a function pointer, function object (or boost lambda) to std::sort to

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.