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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:05:22+00:00 2026-05-22T16:05:22+00:00

So I have a class in java which I group variables together to create

  • 0

So I have a class in java which I group variables together to create my object like so:

public class ExampleClass
{
    private String name;
    private String test;
    private int etc;

    public ExampleClass()
    {
    }

    public String getName()
    {
        return name;
    }

    public void setName(String newName)
    {
        name = newName;
    }

    // ... 
}

Then I create an array or list of these objects, but for this question we will say an array:

int counter = 10;
ExampleClass e[] = new ExampleClass[counter];

for(int i = 0; i < counter; i++)
{
    ExampleClass e2 = ExampleClass();
    e2.setName("a name"); // string grabbed from some other source...
    // ...

    e[i] = e2;
}

And then I fill the values with data which can be anything. Now my question is, how can I sort my array (or list) by alphabetical order of one of the variables, such as name? I’ve used Arrays.sort(theArray) before for when it’s a String[] but I can’t quite work out how to do this with my custom class.

Just to note, this is all in java for the Android but the platform shouldn’t matter.

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

    You can create a Comparator for your ExampleClass and use SortedSet to keep your objects in. In this way your objects will always be sorted. For example,

    public class EComparator implements Comparator<ExampleClass> {
    
      public int compare(ExampleClass o1, ExampleClas o2) {
        return o1.getName().compareTo(o2.getName);
      }
    
    }
    

    And then use TreeSet to auto-sort your examples:

    SortedSet<ExampleClass> data = new TreeSet<ExampleClass>(new EComparator());
    data.add(e1);
    data.add(e2);
    data.add(e3);
    data.add(e4);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a java class which is package-private (declared with class, not public
I have a MATLAB class which contains a reference to a java object classdef
like in java I have: Class.getSuperClass().getDeclaredFields() how I can know and set private field
I have the following class in Java which prints Hello World in portuguese: public
I have a Java string which looks like this, it is actually an XML
I have a Java class that looks like this: public class My_ABC { int
I have an abstract Class Monitor.java which is subclassed by a Class EmailMonitor.java .
I have a java class which fires custom java events. The structure of the
I have a class User with one field called birthDate which is a java.sql.Date
I have an application which resides in ROOT. This application has a java class(in

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.