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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:31:26+00:00 2026-05-27T11:31:26+00:00

Using Collections to sort is nifty, a lot better for me than using Comparator

  • 0

Using Collections to sort is nifty, a lot better for me than using Comparator since i have multiple values that are the same and i’d rather them not be just thrown out to the trash. But Collections has it’s own issue, it seems to think repeating numbers of groups of 2+ to be smaller than their actual smaller counter parts

Example have these keys and values(“katy 1″,”mark 9″,”john 2″,”alice 11″,”josiah 22″,”chris 44”) and it sorts them as follows

alice 11
katy 1
john 2
josiah 22
chris 44
mark 9

Instead of the correct order
katy 1
john 2
mark 9
alice 11
josiah 22
mark 44

How can i fix this?

  • 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-27T11:31:27+00:00Added an answer on May 27, 2026 at 11:31 am

    Since you are passing strings, the collection has no way of telling how you want these strings to be interpreted (i.e. sort by the number present inside the string). You must be more explicit.

    You have two options basically:

    Option 1: Create a new data type to encapsulate a name and a number and implement comparison by number:

    public class Person implements Comparable<Person> {
    
         private String name;
         private int number;
    
         public Person(String name, int number) {
             this.name = name;
             this.number = number;
         }
    
         public int compareTo(Person p) {
             return this.number.compareTo(p.number);
         }
    }
    

    Then:

     List<Person> persons = new ArrayList<Person>();
     persons.add(new Person("alice", 11));
     persons.add(new Person("katy", 1));
     // etc.
     Collections.sort(persons);
    

    Option 2: Turn the string into a key-value pair and put it inside a TreeMap, which automatically keeps the values sorted by key:

     TreeMap<Integer, String> map = new TreeMap<Integer, String>();
     map.put(11, "alice");
     map.put(1, "katy");
     // etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am calling Collections.sort() on an ArrayList using a Comparator that I declared earlier.
I am using the following java code to sort the values: - Collections.sort(values); It
I have an arraylist<string> of words. I sort it using Collections.sort(wordsList); I'm using this
I have this code (C#): using System.Collections.Generic; namespace ConsoleApplication1 { public struct Thing {
I'm using Collections.sort() to sort a LinkedList whose elements implements Comparable interface, so they
I am using a ConcurrentSkipListSet, that is obviously accessed through multiple threads. Now, the
I am using VS2008, and ASP.NET 3.5 C# I have an array list that
I have a class that creates a static array of all properties, using a
I need some data structure that I can build from standard collections or using
Option 1: Make a list which implements Comparable and sort it using collections.sort(List l)

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.