473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to sort an object?

RC
I have simple hashtable object

function myHashTable(key, value) {
this.key = key;
this.value = value;
}

var myObjectArray = new Array(5);

myObjectArray[0] = new myHashTable(1, "2006-06-07");
myObjectArray[1] = new myHashTable(2, "2005-04-10");
.....

Now my question is how do I sort the value, not the key
in myObjectArray?

I did

myObjectArray.sort(myHashTable);

But the output result are NOT correct.

The correct result after sort should be:

myObjectArray[0].key is 2, myObjectArray.value is 2005-04-10
myObjectArray[1].key is 1, myObjectArray.value is 2006-06-07

Thank Q very much in advance!

Jun 7 '06 #1
1 1454
RC <ra**********@noaa.gov> writes:
I have simple hashtable object

function myHashTable(key, value) {
this.key = key;
this.value = value;
}
This is really a hash table entry, not a full hash table?
var myObjectArray = new Array(5);

myObjectArray[0] = new myHashTable(1, "2006-06-07");
myObjectArray[1] = new myHashTable(2, "2005-04-10");
....

Now my question is how do I sort the value, not the key
in myObjectArray?
myObjectArray.sort(functionThatComparesMyHashTable ByValue);

The hard part is making that function :)

A generic comparison function, for anything that works with the less
than operator, is:

function compare(a,b) {
return (a < b) ? -1 : (b < a) ? 1 : 0;
}

You can then build a comparison function for the values:

function functionThatComparesMyHashTableByValue(a,b) {
return compare(a.value, b.value);
}

A shorter name would be recommended :)
I did

myObjectArray.sort(myHashTable);
The argument to the sort function must be a comparison function
returning a number, the sign of that number representing whether
one argument is smaller or bigger than the other.

You are passing a function that takes two arguments and assigns them
to some object (in this case the global object, as you can test by
alerting window.key and window.value afterwards :).
But the output result are NOT correct.


Unsurprisingly! (or really, with only two elements in the array,
there's a 50% chance the sort would be correct if it's random :)

Hope this helps. Code not tested.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
7
by: Diego Barros | last post by:
Hello, I was wondering if it was posibble to sort a map using the values and not the keys. Sorting on the keys (if they were strings, for example) is currently possible. What about, for example,...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
6
by: Der Andere | last post by:
I have an array of pointers (to a class) which I want to have sorted. I have implemented the < operator for the class but I guess STL sort will sort the pointers according to _their_ values (the...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
4
by: DancnDude | last post by:
I have a class that needs to have several different kinds of sorting routines on an ArrayList that it needs to conditionally do based upon the data. I have successfully created a class that...
7
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
11
by: Jeff Schwab | last post by:
Would std::sort ever compare an object with itself? I'm not talking about two distinct, equal-valued objects, but rather this == &that. The container being sorted is a std::vector. I've never...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.