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 6208339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:47:30+00:00 2026-05-24T05:47:30+00:00

How to sort list based on its element value? In my unit test class,

  • 0

How to sort list based on its element value?

In my unit test class, I am receiving list which is in sorted form based on one of its element’s value {sortOrder and it has value 1, 2 and 3} now I am comparing returnedList with expectedList to make sure that both list are same and have same elements in same order.

Example of my return and expected list:

expected List = [Code: ANALYST, SortOrder: 2, Code: STREET, SortOrder: 1]

and returned List = [Code: STREET, SortOrder: 1, Code: ANALYST, SortOrder: 2]

So how can i sort the list on SortOrder so that my expected list becomes:

expected List = [Code: STREET, SortOrder: 1, Code: ANALYST, SortOrder: 2]

I cannot use any libraries and this question is related to Compare List Question, any suggestions?

  • 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-24T05:47:30+00:00Added an answer on May 24, 2026 at 5:47 am

    use this:

     public static void sort(List list, Comparator c)
    

    So do something like this:

    Collections.sort(myList, myComparator)
    

    myComparator is an interface for which you only have to implement one method which is compare:

    public int compare(Object o1, Object o2)
    

    So you can do this with an anonymous inner class:

    Collections.sort(myList, new Comparator<MyType>(){ 
        public int compare(MyType o1, MyType o2) { 
            //My implementation
        }
     });
    

    For information on how to implement the compare method take a look at the documentation here:

    Comparator

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

Sidebar

Related Questions

In my unit test class, I am having two list. One is the expectedValue
I want to sort a list of strings based on the string length. I
I have a list of objects I wish to sort based on a field
How do I sort a list of dictionaries by a specific key's value? Given:
I am trying to sort a dict based on its key and return an
Given the test case below how can I: Sort the IList<TestObject> based on the
I have a list of files I would like to sort based on the
I am trying to sort a singly linked list which has been created and
How would I sort this? >>> list = ["a_0","a_1","a_2","a_3","a_10","a_11","a_23","a_5","a_6","a_5"] >>> sorted(list) ['a_0', 'a_1', 'a_10',
I noticed the specificaition for Collections.sort: public static <T> void sort(List<T> list, Comparator<? super

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.