Solved

Sorting a hashtable in javascript

Posted on 2012-03-28
6
252 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 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

20 Experts available now in Live!

Get 1:1 Help Now