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

The Archive Base Latest Questions

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

Let me give some code so you can see what I’m doing with the

  • 0

Let me give some code so you can see what I’m doing with the following java code for android. Say for example I have the following two classes, one extended from the other:

class MyClassOne {
    protected float x, y;
    MyClassOne(float x, float y) { 
        this.x = x; this.y=y;
    }
    public void printY(){
        System.out.print(y); 
    }
}

class MyClassTwo extends MyClassOne {
    protected String stringSpecificToThisClass;
    private long longSpecificTothisClass;
    MyClassTwo(float x, float y, String s, long l) {
        this.x=x; this.y=y; 
        this.longSpecificTothisClass= l; this.stringSpecificTothisClass=s;
    }
}

These classes are then initialized in the following way

private ArrayList<MyClassOne> mClassOne = new ArrayList<MyClassOne>();
private ArrayList<MyClassTwo> mClassTwo = new ArrayList<MyClassTwo>();
for(int i=0;i<5;i++){
    Random random = new Random(10);
    mClassOne.add(new MyClassOne(i*12, random.nextInt()));
    mClassTwo.add(new MyClassOne(i*11, random.nextInt()));
}

now, what I want to do is compare and sort both arraylists according to the value of y.

The way i do this for a single list is like so:

private Object[][] mSort(){
    Object[][] mSort = new Object[mClassOne.size()][2];
    for(int i = 0; i<mClassOne.size(); i++){
        mSort[i][0] = i;
        mSort[i][1] = mClassOne.get(i).y;
    }
    Arrays.sort(mSort, new Comparator<Object[]>(){
        @Override
        public int compare(Object[] obj1, Object[] obj2){
            Float comp1 = (Float)obj1[1]; Float comp2 = (Float) obj2[1];
            return comp1.compareTo(comp2);
        }
    });
    return mSort;
}

Object[][] mSort = mSort();
for(int i=0;i<mClassOne.size();i++){
    int z = (Integer)mSort[i][0];
    mClassOne.get(z).printY();
}

which could output something like this:

2, 4, 5, 6, 9

Hopefully the code above is clear enough so others can see what I’m trying to do; the question is:

“How could I combine both ArrayLists then sort them by their respective y value.”

The Answer I was looking for

ArrayList<MyClassOne> mTest = new ArrayList<MyClassOne>();
mTest.addAll(mClassOne);
mTest.addAll(mClassTwo);
Collections.sort(mTest, new Comparator<MyClassOne>(){
    @Override
    public int compare(MyClassOne obj1, MyClassTwo obj2) {
        return (int) (obj1.getY() - obj2.getY()); } 
    }
);
// mTest is now sorted, verified by ~ foreach(mTest) {print mTest.getY(); }
  • 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-26T10:04:28+00:00Added an answer on May 26, 2026 at 10:04 am

    Well, you could use a Comparator<MyClassOne> which should be able to handle MyClassTwo instances as well, since MyClassTwo extends MyClassOne.

    Just create a single List<MyClassOne>, add all elements of the other lists and sort.

    In cases where the classes don’t extend each other, introduce a common interface.

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

Sidebar

Related Questions

I have a problem in my MATLAB code. Let me first give you some
Let's say you have a class called Customer, which contains the following fields: UserName
Let me give my question some context. I have a JavaScript widget. The widget
I have problem with validation a very specific beans. Let me give you some
Ok, the question might not be crystal clear. Let me give some details: Let's
Let's say I'm building a data access layer for an application. Typically I have
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
I have an issue with this jquery code below. Please give me advice or
Both user voice and google analytics give some small javascript code which internally loads

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.