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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:47:55+00:00 2026-06-02T23:47:55+00:00

My question is about how to sort an ArrayList with custom Object by one

  • 0

My question is about how to sort an ArrayList with custom Object by one of them properties but starting from a custom condition.

Let me explain better, here is my code:

public static void sortArrayListByProperty(ArrayList colorList){

        Collections.sort(colorList, new Comparator(){

            public int compare(Object emp1, Object emp2){

                int intValue1 = ((ColorCounter)emp1).getIntColorValue();        
                int intValue2 = ((ColorCounter)emp2).getIntColorValue();

                if(intValue1 < intValue2)
                    return 1;
                else if(intValue1 > intValue2)
                    return -1;
                else
                    return 0;    
            }
        });
    }

This will sort my ArrayList from bigger to smaller.

But what I want is to sort my ArrayList from a starting number that I will specify.

For example if ArrayList contains

5 3 9 1 14 

lets say that I want numbers start from 3 then I need to have

3 5 9 14 1

I hope is clear enought…

Is it possible?

@Joachim Sauer

Thank you, I edited your code a little and changed return values and it worked!

Edited code:

if (cv1 >= threshold && cv2 < threshold) {
   return -1;
} else if (cv2 >= threshold && cv2 < threshold) {
   return -1;
} else if (cv1 < cv2) {
   return 1;
} else if (cv1 > cv2) {
   return 1;
} else {
   return 0;    
}

Test example:

16777215
16448250
15790320
4013373

Sorting by 15790320:

15790320
16448250
16777215
4013373
  • 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-06-02T23:47:56+00:00Added an answer on June 2, 2026 at 11:47 pm

    You could try this:

    public class ColorCounterComparator implements Comparator<ColorCounter> {
      private final threshold;
    
      public ColorCounterComparator(final int threshold) {
        this.threshold = threshold;
      }
    
      @Override
      public int compare (ColorCounter c1, ColorCounter c2) {
        int cv1 = c1.getIntColorValue();
        int cv2 = c1.getIntColorValue();
    
        if (cv1 >= threshold && cv2 < threshold) {
           return -1;
        } else if (cv2 >= threshold && cv2 < threshold) {
           return 1;
        } else if (cv1 < cv2) {
           return -1;
        } else if (cv1 > cv2) {
           return 1;
        } else {
           return 0;    
        }
      }
    }
    

    This is obviously very untested, might have some off-by-one-errors and might have flipped the -1/1 values. But it should show you the basic idea 😉

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

Sidebar

Related Questions

This is sort of the Java analogue of this question about C# . Suppose
I have been asked to post a new question about how to correctly sort
Happy easters, everyone. I am currently learning topological sort and having a question about
So this is sort of a question about a posted solution. I was trying
Hi I have a question about Batcher's odd-even-merge sort. I have the following code:
This is not another question about 'How Can I Sort Dynamically (based on an
I have a sort of general question about the geocoder gem and how to
I have a language-agnostic question about an algorithm. This comes from a (probably simple)
I have a question about sort defaultdict in python Assume I have the following
I have a question about precompiling ASP.NET web application projects from TeamCity. This is

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.