Solved

Sorting a hashtable in javascript

Posted on 2012-03-28
6
311 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
 

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
Technology Partners: 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: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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

696 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