Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Sort Array while maintain its index

Reply
Thread Tools

Sort Array while maintain its index

 
 
Rahul
Guest
Posts: n/a
 
      06-08-2009
hello ,

I have a array that I want to sort , the sorted array will be in asc
form and array keys will be maintained . Can anybody help me ?


states_array['1'] = 'Canada';
states_array['2'] = 'Netherland';
states_array['5'] = 'Africa';
states_array['3'] = 'India';
states_array['4'] = 'USA';

and the result after sorting will be

states_array['5'] = 'Africa';
states_array['1'] = 'Canada';
states_array['3'] = 'India';
states_array['2'] = 'Netherland';
states_array['4'] = 'USA';

Thanks,
Rahul
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      06-08-2009
Rahul wrote:
> I have a array that I want to sort , the sorted array will be in asc
> form


What is `asc form'?

> and array keys will be maintained


An ECMAScript Array has properties with numeric name as indexes, not keys.
An ECMAScript Object has properties with arbitrary name, not keys.

> . Can anybody help me ?
>
> states_array['1'] = 'Canada';


I told why it is not necessary to quote a numeric property name.

> states_array['2'] = 'Netherland';
> states_array['5'] = 'Africa';
> states_array['3'] = 'India';
> states_array['4'] = 'USA';
>
> and the result after sorting will be
>
> states_array['5'] = 'Africa';
> states_array['1'] = 'Canada';
> states_array['3'] = 'India';
> states_array['2'] = 'Netherland';
> states_array['4'] = 'USA';


That is semantically equivalent. No sorting necessary.

The point of sorting is that the key for a value, or the value for a key,
*changes* according to the sort order.


PointedEars
 
Reply With Quote
 
 
 
 
ace
Guest
Posts: n/a
 
      06-08-2009
Rahul wrote:
> hello ,
>
> I have a array that I want to sort , the sorted array will be in asc
> form and array keys will be maintained . Can anybody help me ?
>
>
> states_array['1'] = 'Canada';
> states_array['2'] = 'Netherland';
> states_array['5'] = 'Africa';
> states_array['3'] = 'India';
> states_array['4'] = 'USA';
>
> and the result after sorting will be
>
> states_array['5'] = 'Africa';
> states_array['1'] = 'Canada';
> states_array['3'] = 'India';
> states_array['2'] = 'Netherland';
> states_array['4'] = 'USA';


You want to do sorting on object properties; are you sure you want to do
this?

 
Reply With Quote
 
Matthias Reuter
Guest
Posts: n/a
 
      06-08-2009
Rahul wrote:

> hello ,
>
> I have a array that I want to sort , the sorted array will be in asc
> form and array keys will be maintained . Can anybody help me ?
>
>
> states_array['1'] = 'Canada';
> states_array['2'] = 'Netherland';
> states_array['5'] = 'Africa';
> states_array['3'] = 'India';
> states_array['4'] = 'USA';
>
> and the result after sorting will be
>
> states_array['5'] = 'Africa';
> states_array['1'] = 'Canada';
> states_array['3'] = 'India';
> states_array['2'] = 'Netherland';
> states_array['4'] = 'USA';
>
> Thanks,
> Rahul



You have to store the indexes explicitly, for example like that:

var states = [
{
index : 1,
region : "Canada"
},
{
index : 2,
region : "Netherlands"
},
{
index : 3,
region : "Africa"
},
{
index : 4,
region : "India"
},
{
index : 5,
region : "USA"
}
];

To sort this array by key, you could do something like this

/*
* returns a sort function
*/
var sortByKey = function (key) {
return function (a, b) {
if (a && typeof a === "object") {
if (b && typeof b === "object") {
return a[key] - b[key];
}
return 1;
}
return -1;
};
};

states.sort(sortByKey("index"));
states.sort(sortByKey("region"));
 
Reply With Quote
 
ace
Guest
Posts: n/a
 
      06-08-2009
Conrad Lender wrote:
> 2) Do the sorting on the client side. Here's an example implementation:
>
> var sorted = states_array
> .map(function(name, id) { return {id: id, name: name}; })
> .sort(function(a, b) { return a.name > b.name ? 1 : -1 })
> .filter(function(ele) { return ele; });


It seems that filter isn't doing much here?
 
Reply With Quote
 
ace
Guest
Posts: n/a
 
      06-08-2009
Conrad Lender wrote:
>>> var sorted = states_array
>>> .map(function(name, id) { return {id: id, name: name}; })
>>> .sort(function(a, b) { return a.name > b.name ? 1 : -1 })
>>> .filter(function(ele) { return ele; });

>> It seems that filter isn't doing much here?

>
> The OP had an array with an undefined 0th element; I used filter() to
> remove this element from the result. Looking at it again, it would
> probably be more efficient to filter it out before the sort().


Yes, filtering should be as soon as possible, but
console.dir(states_array) doesn't show 0-th element?
 
Reply With Quote
 
ace
Guest
Posts: n/a
 
      06-08-2009
Conrad Lender wrote:
>>> remove this element from the result. Looking at it again, it would
>>> probably be more efficient to filter it out before the sort().

>> Yes, filtering should be as soon as possible, but
>> console.dir(states_array) doesn't show 0-th element?

>
> Try console.log(states_array).


Tnx.
 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      06-09-2009
On Jun 8, 5:54*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> Rahul wrote:
> > I have a array that I want to sort , the sorted array will be in asc
> > form

>
> What is `asc form'?
>
> > and array keys will be maintained

>
> An ECMAScript Array has properties with numeric name as indexes, not keys..
> An ECMAScript Object has properties with arbitrary name, not keys.
>
> > . Can anybody help me ?

>
> > states_array['1'] = 'Canada';

>
> I told why it is not necessary to quote a numeric property name.
>
> > states_array['2'] = 'Netherland';
> > states_array['5'] = 'Africa';
> > states_array['3'] = 'India';
> > states_array['4'] = 'USA';

>
> > and the result after sorting will be

>
> > states_array['5'] = 'Africa';
> > states_array['1'] = 'Canada';
> > states_array['3'] = 'India';
> > states_array['2'] = 'Netherland';
> > states_array['4'] = 'USA';

>
> That is semantically equivalent. *No sorting necessary.
>
> The point of sorting is that the key for a value, or the value for a key,
> *changes* according to the sort order.
>
> PointedEars


Explanation seems to be pure rubbish.
 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      06-09-2009
On Jun 8, 7:18*pm, Conrad Lender <crlen...@yahoo.com> wrote:
> On 08/06/09 14:21, Rahul wrote:
>
> > I have a array that I want to sort , the sorted array will be in asc
> > form and array keys will be maintained . Can anybody help me ?

>
> See Thomas's post about the difference between objects and arrays.
>
> > states_array['5'] = 'Africa';
> > states_array['1'] = 'Canada';
> > states_array['3'] = 'India';
> > states_array['2'] = 'Netherland';
> > states_array['4'] = 'USA';

>
> Just a wild guess... this looks like data to be used in a <select>
> element, and your "array keys" are database record ids. If that's the
> case, you have two options:
>
> 1) Do the sorting on the server side, and use a different data
> structure; for example:
>
> * var states = [
> * * * {id: 5, name: "Africa"},
> * * * {id: 1, name: "Canada"},
> * * * ... etc
> * ];
>
> Adjust the code that populates the dropdown to work with the new structure.
>
> 2) Do the sorting on the client side. Here's an example implementation:
>
> var sorted = states_array
> * * * * * * .map(function(name, id) { return {id: id, name: name}; })
> * * * * * * .sort(function(a, b) { return a.name > b.name ? 1: -1 })
> * * * * * * .filter(function(ele) { return ele; });
>
> // testing:
> for (var i = 0, len = sorted.length; i < len; ++i) {
> * * console.log(sorted[i].id + ": " + sorted[i].name);}
>
> // * 5: Africa
> // * 1: Canada
> // * 3: India
> // * 2: Netherland
> // * 4: USA
>
> "map" and "filter" are JavaScript 1.5 features, and are not supported by
> older browsers. You can retrofit them using the code snippets from these
> pages:
> <https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Object...>
> <https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Object...>
>
> You can also use traditional loops to prepare the sorted array.
>
> * - Conrad


Thanks Conrad, I am going to use your logic .
One more point , how to convert array format such as

states_array['5'] = 'Africa';
states_array['1'] = 'Canada';

to the format given below . Do you want me to do this at run time?

var states = [
{id: 5, name: "Africa"},
{id: 1, name: "Canada"}
];



 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      06-09-2009
Rahul wrote on 09 jun 2009 in comp.lang.javascript:
> Thomas:
>> The point of sorting is that the key for a value, or the value for a
>> key, *changes* according to the sort order.

>
> Explanation seems to be pure rubbish.


That was to be forseen, as the OQ was pure rubish.

[Garbage in, garbage out, remember.]

states_array['2'] = '';

1 That is not a javascript array, because an array has a numeric index.

2 An array is sorted by it's index.

===================================

If you want another sort, while "maintaining the [numeric] index",
you infact have to maintain the original array,
so you better use another array, an secondary index array:


<script type='text/javascript'>

var theArr = [];
var indexArr = [];
var temp;

theArr[2] = 'Netherland';
theArr[5] = 'Africa';
theArr[3] = 'India';
theArr[4] = 'USA';

// fill indexArr
var y = 0;
for(var x = 0; x < theArr.length; x++) {
if (theArr[x] != undefined) {
indexArr[y++] = x;
};
};

// bubblesort indexArr
for(x = 0; x < indexArr.length; x++) {
for(y = 0; y < indexArr.length-1; y++) {
if (theArr[indexArr[y]] > theArr[indexArr[y+1]]) {
temp = indexArr[y+1];
indexArr[y+1] = indexArr[y];
indexArr[y] = temp;
};
};
};

// show
for(x = 0; x < indexArr.length; x++) {
document.write(x + ': index: ' + indexArr[x]);
document.write(' value: ' + theArr[indexArr[x]] + '<br>');
};

</script>

==========================

This should work too, but I get an upsifde down result:

var indexArr = indexArr.sort(sortFunct);

function sortFunct(b,a) {
return (theArr[a] > theArr[b]);
};

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57