Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anyone know how to sort a collection 1

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
I know you can do this by it's key, but what if I want to treat it like a vertual database where I can sort it by it's object's properties. The door to life is never locked, but few have the knowledge to open it.
 
Bigfoot -

You can sort a collection, but it ain't pretty. If you've got lots of memory available, just read it out of collection A into B in the order you want. If you want to do an in-place sort, then things get really ugly. You'd have to loop through the collection re-assigning the key values so that they appear in correct order. You could do an insertion sort, but modified where when you need to swap two items, you'd copy them out to temp object variables, delete the items from the collection, swap their key values, then add them back in. When you get done, you could iterate through the collection by key value to get them in ascending order.

If you don't have a copy already, pick up:
Practical Algorithms for Programmers
Paperback, 592 Pages, Edition Number 01, Addison-Wesley Longman, Incorporated, June 1995
ISBN: 020163208X

The algorithms in the book are written in C, but they're very readable, since the author doesn't do any tricks with pointers. This book is highly reccommended.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top