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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:16:24+00:00 2026-05-12T16:16:24+00:00

I have an unsorted linked list. To sort it, I thought I’d put the

  • 0

I have an unsorted linked list. To sort it, I thought I’d put the values into a TreeSet with a comparator supplied, then return those values as a new linked list. Yet, it fails.

Comparator:

public class SortSpeciesByCommonName implements Comparator<Species> {

    /**
     * a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. 
     */
    @Override
    public int compare(Species arg0, Species arg1) {
        return arg0.getName().compareTo(arg1.getName()); //arg.getName() is String
    }

}

Sorting function:

public static LinkedList<Species> sortedAnimals(LinkedList<Species> animals) {
    TreeSet<Species> sortedBreeds = new TreeSet<Species>(new SortSpeciesByCommonName());
    sortedBreeds.addAll(animals);
    return new LinkedList<Species>(sortedBreeds);
}

When testing the values, everything appears to still be in insertion order.

  • 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-12T16:16:25+00:00Added an answer on May 12, 2026 at 4:16 pm

    Why don’t you use Collections.sort(List,Comparator):

    LinkedList<Species> sorted = new LinkedList<Species>(arg);
    Collections.sort(sorted, new Comparator<Species>() {
      @Override
      public int compare(Species s1, Species s2) {
          return s1.getName().compareTo(s2.getName());
      }
    });
    

    We cannot really debug your program and why the list isn’t sorted. Can you provide a test case? What’s the signature of Species.getName()? Is it a String?

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

Sidebar

Related Questions

I have an unsorted linked list. I need to sort it by a certain
How to sort list of date object ? For example I have unsorted list
I have an unsorted list but I want to sort in a custom way
I have a nested unsorted list and I want to place a click event
I have an unsorted UL list displaying search results. I want the user to
I have an unsorted list ( UL ) that contains sortable list items (
I have a large unsorted list of items. Some items are important and need
I have implemented an insertion sort in a double link list (highest to lowest)
I have an unsorted dictionary file named dict.txt. I have managed to put the
I have an unsorted list. I want each list item to display one or

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.