Solved

Sorting an object based on 2 values

Posted on 2006-11-02
5
220 Views
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?

Thanks  

0
Comment
Question by:twibblejaway
  • 3
  • 2
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
0
 

Author Comment

by:twibblejaway
Comment Utility
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?  
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
if you need the original array then take a copy before sorting (or resort it depending on your exact needs)
0
 

Author Comment

by:twibblejaway
Comment Utility
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.  
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to use external config file with Spring MVC 4 58
json example 39 108
topping1 challenge 7 46
pairs challenge 5 44
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now