Solved

Sorting a hashtable in javascript

Posted on 2012-03-28
6
303 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
[X]
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
6 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37775436
0
 

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:gurvinder372
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 167 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 167 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 166 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

734 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