• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

Neophyte

[java] Sorting a Hashtable

9 posts in this topic

Is there any way to sort a hashtable? I don''t necessarily need to sort the actual Hashtable, but I need some way of getting a sorted list of the elements in the Hashtable. Any help would be greatly appreciated. Neophyte - Death awaits you all with nasty, big, pointy teeth. -
0

Share this post


Link to post
Share on other sites
Look up the documentation for the correct method names but i believe hashtable has a method like "hashKeys()" or "keySet()" that will return a set of all the keys of your hash table. you can then iterate through the set. not sure if im answering your question.
0

Share this post


Link to post
Share on other sites
You can use my sorttool package. It will only sort Object arrays for compatibility sake, but you can get an Object array from a hashtable like so:

Object [] list = hash.getValues().toArray();

http://www.tapinternet.com/~mark/projects/sorttool.zip
0

Share this post


Link to post
Share on other sites
wikidsmot:
The problem is that Hashtable doesn''t guarantee that the Set returned by keySet() is in any particular order.

Jim_Ross:
That code looks pretty good, thanks.
But it''s probably overkill for me to use code that is that generic, so is it OK for you if I tweak it to fit my own purposes and pass it of as my own? (Just kidding, of course. The question (in all seriousness) is if you mind if I use your code as a base from which to make my own implementation. This *is* for use in a commercial product, by the way).


Neophyte


- Death awaits you all with nasty, big, pointy teeth. -
0

Share this post


Link to post
Share on other sites
Assuming your keys were of class Key, which implements Comparable, just do something simple like this:


Key [] sortedKeys = (Key [])Arrays.sort(htable.keySet().toArray());


Arrays also has a sort(Object [], java.util.Comparator) method, if your key objects don't implement Comparable.

Edited by - c_wraith on January 4, 2001 5:42:32 AM
0

Share this post


Link to post
Share on other sites
you could use a treemap so that it is always sorted
0

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster

you could use a treemap so that it is always sorted


I agree with this one. If you need to do sorting then it might be the wrong datastructure you have chosen. For frequent sorting an orderedmap/treemap is a better solution - off coarse this depends o how often you do a sorting compared to inserting and looking up in the table.

Always choose the right dfata structure for the job. No data structure can do it all.

Jacob Marner
0

Share this post


Link to post
Share on other sites
BTW,

If you use many kinds of containers and standard algorithms I can highly recommend the free JGL library from objectspace.

Check it out at

www.objectspace.com

I use it all the time.

Jacob Marner
0

Share this post


Link to post
Share on other sites
Unfortunately, due to the nature of the application I am making, I don''t really have much choice but to use a Hashtable (I am building and manipulating a large DOM-tree, and need to be able to quickly locate a particular element in it from a string).

In the end, though, I have now decided not to sort the hashtable, but rather keep a sorted Vector of the keys, and go through the vector to find which key in the Hashtable to get (not a very neat solution, I know, but it works.).
In effect, something like this:

  
Hashtable elements;
Vector sorted;

/* Fill in the Hashtable and Vector */


// To get the 4th element (sorted):

String key = (String) sorted.elementAt(4);
Element elem = (Element) elements.get(key);




As the Vector is created sorted (it is created from a database-call), it is easy to keep it sorted when adding and removing elements.

Anyway, thanks for you help all.

Neophyte

- Death awaits you all with nasty, big, pointy teeth. -
0

Share this post


Link to post
Share on other sites
quote:
Original post by Neophyte
...

As the Vector is created sorted (it is created from a database-call), it is easy to keep it sorted when adding and removing elements.



Haha, ''order by'' in SQL? Awesome, I love easy-ass answers to problems. I don''t mind if you play with the code, just send me back what you have done to it. That goes for anybody.
0

Share this post


Link to post
Share on other sites