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

The Archive Base Latest Questions

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

I have a problem with sorting an ArrayList of my own objects. My class

  • 0

I have a problem with sorting an ArrayList of my own objects.

My class of these objects looks like that:

public class Robal {
    int[] gen = new int[5];
    int fit;
    Random losuj = new Random();

    public Robal(int przelacz) {
        for (int i=0; i<5; i++ ) {
            gen[i]=losuj.nextInt(2);
        }
        fit=dopasuj(gen, przelacz);    
    }

    int dopasuj(int[] gen, int przelacz)    {
        int toDec=(gen[0]*1)+(gen[1]*2)+(gen[2]*4)+(gen[3]*8)+(gen[4]*16);
        return y(toDec, przelacz);
    }

    int y(int toDec, int przelacz) {
        if (przelacz == 1) {
            return toDec*toDec;
        } else if (przelacz == 2) {
            return 1;
        } else if(przelacz == 3) {
            return 1;
        } else if(przelacz == 4) {
            return 1;
        } else {
            return 1;
        }
    }

    void drukuj(int i) {
        System.out.println("Geny Robala "+i+": "+gen[0]+" "+gen[1]+" "+gen[2]+" "+gen[3]+" "+gen[4]+"\tfit:"+fit);
    }
}

and My Main class is:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class PE {

public static void main (String[] args){
ArrayList<Robal> Robale = new ArrayList<Robal>();
int i=0;
while(i!=5){
Robale.add(new Robal(1));
i++;
}
Robale.get(0).drukuj(1);
Robale.get(1).drukuj(2);
Robale.get(2).drukuj(3);
Robale.get(3).drukuj(4);

Collections.sort(Robale, new Comparator<Robal>() {
public int compare(Robal a, Robal b){
return a.fit - b.fit;
}
});

Robale.get(0).drukuj(1);
Robale.get(1).drukuj(2);
Robale.get(2).drukuj(3);
Robale.get(3).drukuj(4);


}


}

but most of the time the Data before and after sorting is not the same…
example result:

Geny Robala 1: 0 1 1 0 0 fit:36
Geny Robala 2: 1 0 1 0 1 fit:441
Geny Robala 3: 1 1 0 0 1 fit:361
Geny Robala 4: 0 1 1 1 0 fit:196
Geny Robala 1: 0 1 1 0 0 fit:36
Geny Robala 2: 0 0 0 1 0 fit:64
Geny Robala 3: 0 1 1 1 0 fit:196
Geny Robala 4: 1 1 0 0 1 fit:361
  • 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:06:41+00:00Added an answer on May 27, 2026 at 11:06 am

    Are you sure you are not instantiating new Robal while sorting the list? that would cause the random values to shift

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

Sidebar

Related Questions

I have a problem on using LinqToSQL with paging + dynamic sorting. These are
I have a problem with Gridview sorting that is similar to others but I'm
I have a problem sorting vectors in 2D vector? I would like to sort
I am having a problem sorting results from joining tables that have to be
I have a problem with sorting NSTableColumn contents. In my NSTableView there are three
I have a problem with sorting items in table in PHP. Here is what
I have a complex sorting problem with my SQL statement. I have a table
I have a problem with how the List Sort method deals with sorting. Given
I am currently faced with a difficult sorting problem. I have a collection of
I have problem in some JavaScript that I am writing where the Switch statement

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.