[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Sorting a hashtable in javascript

Posted on 2012-03-28
6
Medium Priority
?
330 Views
Last Modified: 2012-03-28
I have a hashtable of this form:
ht['1'] = '23'
ht['2'] = '53'
ht['3'] = '45'

I need to sort it in ascending order:
ht['1'] = '23'
ht['3'] = '45'
ht['2'] = '53'

Please let me know how this can be done. Thanks!
0
Comment
Question by:dshrenik
6 Comments
 

Author Comment

by:dshrenik
ID: 37775451
I saw that link. But I was not entirely sure how I can relate to it since it uses a different format.
Moreover, I tried running the solution, but it did not work.
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 37775470
you are basically sorting the values in an array, for which you can simply take out all the values in a separate array and then sort the same
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 668 total points
ID: 37775484
var newArray = [];

for ( x in ht )
{
   newArray [ newArray.length ] = parseInt(ht[x]);
}
newArray.sort();

var ht2 = new Array();
for ( var counter = 0; counter < newArray.length; counter++)
{
   while (x in ht)
   {
      if ( ht[x] == newArray[counter])
      {
          ht2 [x] =  ht[x];
          break;
      }
   }
}

return ht2;
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 668 total points
ID: 37775519
Object.prototype.iterateSorted = function(worker)
{
    var keys = [];
    for (var key in this)
    {
        if (this.hasOwnProperty(key))
            keys.push(key);
    }
    keys.sort();

    for (var i = 0; i < keys.length; i++)
    {
        worker(this[key]);
    }
}
var myObj = { a:1, b:2 };
myObj.iterateSorted(function(value)
{
    alert(value);
} 

Open in new window

0
 
LVL 5

Assisted Solution

by:Ronak Patel
Ronak Patel earned 664 total points
ID: 37776004
<html>
<head>
<script language="javascript">

var ht = new Object;

ht['1'] = '23'
ht['2'] = '53'
ht['3'] = '45'
ht['5'] = '1'
ht['4'] = '35'

sortByValue(ht, function(key, value) {document.write("ht['" + key + "'] = " + value + "<br>"); });

function sortByValue(obj, callback, context) 
{
	var tuples = [];      
	for (var key in obj)
	{
		tuples.push([key, obj[key]]);
	}
	
	tuples.sort(function(a, b) { return a[1] < b[1] ? 1 : a[1] > b[1] ? -1 : 0 });
	var length = tuples.length;
	while (length--)
	{
		callback.call(context, tuples[length][0], tuples[length][1]); 
	}
}
</script>
</head>
</html>

Open in new window


Output:
ht['5'] = 1
ht['1'] = 23
ht['4'] = 35
ht['3'] = 45
ht['2'] = 53
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

831 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