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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:05:08+00:00 2026-05-15T18:05:08+00:00

I have an ArrayList which I need to sort and also I need a

  • 0

I have an ArrayList which I need to sort and also I need a specific default item to be on top of the list. I can do that myself by checking the array list, deleting the default item, and inserting it at the top of the list.

Basically the list should have a default value on top and remaining values in sorted order. Is there is any existing API method that does 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-15T18:05:09+00:00Added an answer on May 15, 2026 at 6:05 pm

    I’d just write a method that did what you describe… something like:

    public static <T> void sortWithDefault(final List<T> list)
    {
        Collections.sort(list);
        // remove the default item
        // insert the default item at the start of the list
    }
    

    You could also make use of a Comparator (it would be less obvious though) where it always compares the default item as the lowest in any comparison and the natural comparison for the rest of the items. For that you would call Collections.sort(List, Comparator);

    Here is some code for such a Comparator… again. I don’t really like it because it isn’t obvious… but it is a reasonable solution I guess:

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class Main
    {
        public static void main(final String[] argv)
        {
            final List<Integer> list;
    
            list = new ArrayList<Integer>();
    
            for(int i = 10; i > 0; i--)
            {
                list.add(i);
            }
    
            Collections.sort(list, new DefaultAtStartComparator<Integer>(5));
    
            System.out.println(list);
        }
    }
    
    class DefaultAtStartComparator<T extends Comparable>
        implements Comparator<T>
    {
        private final T defaultValue;
    
        public DefaultAtStartComparator(final T value)
        {
            defaultValue = value;
        }
    
        public int compare(final T a,
                           final T b)
        {
            if(a.equals(defaultValue))
            {
                return (-1);
            }
    
            if(b.equals(defaultValue))
            {
                return (1);
            }
    
            return (a.compareTo(b));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 467k
  • Answers 467k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should simply: create navigation controller; set navigationController.view.frame to necessary… May 16, 2026 at 1:53 am
  • Editorial Team
    Editorial Team added an answer ok, i just answer this question then close it what… May 16, 2026 at 1:53 am
  • Editorial Team
    Editorial Team added an answer Try just adding your assignment to your javascript statement: page… May 16, 2026 at 1:53 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.