Solved

Sorting a hashtable in javascript

Posted on 2012-03-28
6
296 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Web Development 6 19
Getting selected value using Jquery 3 20
Print a <div></div> only 5 34
Ajax on ASP 2 35
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

735 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