Sorting an object based on 2 values

Posted on 2006-11-02
Last Modified: 2010-03-31
Hello, I have an array of objects with 2 fields: String Key, and int dID.

What I want to do is sort the array first by the Key, and then by the dID.

for example:

"Hello", 7
"Hello", 4
"Apple", 9

Becomes:  "Apple", 9
                "Hello", 4
                "Hello", 7

Before I start sorting, the array is already sorted by dID, so I am wondering if there is a sorting algorithm that will sort the Keys, while keeping the dID's in order like my example above.

If there is no such algorithm, what is the best method to go about achieving this?


Question by:twibblejaway
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 92

Accepted Solution

objects earned 500 total points
ID: 17863529

Author Comment

ID: 17863772
ok, so i have that working, and I can print out the list to verify that it is sorted appropriatly.  How can I get my original object array back now though?  I dont even need to get it back as an array, is there just a way I can remove the object one at a time?  
LVL 92

Expert Comment

ID: 17863788
if you need the original array then take a copy before sorting (or resort it depending on your exact needs)

Author Comment

ID: 17863790
I dont need the original.  What I need is to be able to take 1 element out of the newly sorted array at a time.  
LVL 92

Expert Comment

ID: 17863813
better off using a List if you want to be removing elements.
With an array best you can do is set the element to null.

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 73
jsp login check 12 61
Bot application - advice 3 78
Java class and jar 3 55
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question