Sort Array while maintain its index

Discussion in 'Javascript' started by Rahul, Jun 8, 2009.

  1. Rahul

    Rahul Guest

    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
     
    Rahul, Jun 8, 2009
    #1
    1. Advertising

  2. 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
     
    Thomas 'PointedEars' Lahn, Jun 8, 2009
    #2
    1. Advertising

  3. Rahul

    ace Guest

    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?
     
    ace, Jun 8, 2009
    #3
  4. 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"));
     
    Matthias Reuter, Jun 8, 2009
    #4
  5. Rahul

    ace Guest

    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? :)
     
    ace, Jun 8, 2009
    #5
  6. Rahul

    ace Guest

    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?
     
    ace, Jun 8, 2009
    #6
  7. Rahul

    ace Guest

    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.
     
    ace, Jun 8, 2009
    #7
  8. Rahul

    Rahul Guest

    On Jun 8, 5:54 pm, Thomas 'PointedEars' Lahn <>
    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.
     
    Rahul, Jun 9, 2009
    #8
  9. Rahul

    Rahul Guest

    On Jun 8, 7:18 pm, Conrad Lender <> 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.id + ": " + sorted.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"}
    ];
     
    Rahul, Jun 9, 2009
    #9
  10. Rahul

    Evertjan. Guest

    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);
    };

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jun 9, 2009
    #10
  11. Rahul wrote:
    > Thomas 'PointedEars' Lahn wrote:
    >> [snipped rest of full quote]
    >> The point of sorting is that the key for a value, or the value for a key,
    >> *changes* according to the sort order.
    >> [...]


    The last time you have been asked to quote properly according to what also
    the FAQ of this newsgroup recommends has already passed. FOAD.

    > Explanation seems to be pure rubbish.


    As did your question.

    *PLONK*


    PointedEars
     
    Thomas 'PointedEars' Lahn, Jun 9, 2009
    #11
  12. "Evertjan." <> writes:

    > [Garbage in, garbage out, remember.]
    >
    > states_array['2'] = '';
    >
    > 1 That is not a javascript array, because an array has a numeric index.


    Actually not.
    All object properties are strings, not numbers, including array indices.
    It's just that array indices exactly the result of doing toString on values
    in the range 0..2^32-1.

    > 2 An array is sorted by it's index.


    Hmm. Either it's a tautology, or it's just meaningless.
    An array isn't sorted any way. Neither are its properties, necessarily.
    You can order the properties numerically, but that's not a property of
    the array.

    ....

    > This should work too, but I get an upsifde down result:
    >
    > var indexArr = indexArr.sort(sortFunct);
    >
    > function sortFunct(b,a) {
    > return (theArr[a] > theArr);
    > };


    That won't work. The comparison function should return positive, zero
    or negative depending on the relative order of its argument. This
    function only returns a boolean value, which converts to either 0 or 1
    as a number, so it can't represent that b is less than a.

    A comparison function that does work would be

    function cmp(a, b) { // Generic comparison function compatible with '<'
    return (b < a) - (a < b); // Obscure, but it works.
    }

    // Reversing comparison:
    function sortFunct(a, b) {
    return cmp(theArr, theArr[a]);
    }

    Then you can do:
    indexArr.sort(sortFunct);
    (No need to assign it back to indexArr. The sort function updates the array
    in-place).


    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
     
    Lasse Reichstein Nielsen, Jun 9, 2009
    #12
  13. Rahul

    Evertjan. Guest

    Lasse Reichstein Nielsen wrote on 09 jun 2009 in comp.lang.javascript:

    > "Evertjan." <> writes:
    >
    >> [Garbage in, garbage out, remember.]
    >>
    >> states_array['2'] = '';
    >>
    >> 1 That is not a javascript array, because an array has a numeric
    >> index.

    >
    > Actually not.
    > All object properties are strings, not numbers, including array
    > indices. It's just that array indices exactly the result of doing
    > toString on values in the range 0..2^32-1.


    But an array is not just an object, the numeric indices give special end
    extra seriality to the numeric behavour of the array.

    That an array also exhibit "normal" object behavour is a pity or an
    advantage in Javascript, depending on your mood.

    >> 2 An array is sorted by it's index.

    >
    > Hmm. Either it's a tautology, or it's just meaningless.


    You should make up your mind.
    It may be a tautology in the sense that you alreaduy knew it was so, and
    nit different, but wether or not it is not meaningless.

    If you sort an array, the indices get new values, so that the order of
    indices remains the same amd the values are sorted, bubbled or otherwise.

    If you want to sort an array without changing the inex-value pairing, you
    will have to define a new array for indirect indexing.

    Sorting without assigning indices is nonsense.


    > An array isn't sorted any way. Neither are its properties,
    > necessarily. You can order the properties numerically, but that's not
    > a property of the array.


    an array in sense strictior has no properties, that is object behavour,
    and a javascript array has object behavour.


    >> This should work too, but I get an upsifde down result:
    >>
    >> var indexArr = indexArr.sort(sortFunct);
    >>
    >> function sortFunct(b,a) {
    >> return (theArr[a] > theArr);
    >> };

    >
    > That won't work. The comparison function should return positive, zero
    > or negative depending on the relative order of its argument. This
    > function only returns a boolean value, which converts to either 0 or 1
    > as a number, so it can't represent that b is less than a.


    Ah yes, true.

    > A comparison function that does work would be
    >
    > function cmp(a, b) { // Generic comparison function compatible with
    > '<'
    > return (b < a) - (a < b); // Obscure, but it works.
    >}
    >
    > // Reversing comparison:
    > function sortFunct(a, b) {
    > return cmp(theArr, theArr[a]);
    >}
    >
    > Then you can do:
    > indexArr.sort(sortFunct);


    Yes!

    > (No need to assign it back to indexArr. The sort function updates the
    > array in-place).


    Sorry? No, not according to the OP's wishes.

    So try this, OP:

    =============================================
    <script type='text/javascript'>

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

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

    // fill indexArr with pointers, skipping not assigned values
    var y = 0;
    for(var x = 0; x < theArr.length; x++) {
    if (theArr[x] != undefined) {
    indexArr[y++] = x;
    };
    };

    // indirect index sort
    var indexArr = indexArr.sort(sortFunct);

    // inner sort function declaration
    function sortFunct(a,b) {
    return (theArr[a] > theArr)? 1:(theArr[a] < theArr)?-1:0;
    }


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

    </script>
    ==========================================

    Now the indexArr has values
    pointing to the theArr indices
    in the order of the sorted theArr.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jun 9, 2009
    #13
  14. Rahul

    Rahul Guest

    On Jun 10, 12:02 am, "Evertjan." <>
    wrote:
    > Lasse Reichstein Nielsen wrote on 09 jun 2009 in comp.lang.javascript:
    >
    > > "Evertjan." <> writes:

    >
    > >> [Garbage in, garbage out, remember.]

    >
    > >> states_array['2'] = '';

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

    >
    > > Actually not.
    > > All object properties are strings, not numbers, including array
    > > indices. It's just that array indices exactly the result of doing
    > > toString on values in the range 0..2^32-1.

    >
    > But an array is not just an object, the numeric indices give special end
    > extra seriality to the numeric behavour of the array.
    >
    > That an array also exhibit "normal" object behavour is a pity or an
    > advantage in Javascript, depending on your mood.
    >
    > >> 2 An array is sorted by it's index.

    >
    > > Hmm. Either it's a tautology, or it's just meaningless.

    >
    > You should make up your mind.
    > It may be a tautology in the sense that you alreaduy knew it was so, and
    > nit different, but wether or not it is not meaningless.
    >
    > If you sort an array, the indices get new values, so that the order of
    > indices remains the same amd the values are sorted, bubbled or otherwise.
    >
    > If you want to sort an array without changing the inex-value pairing, you
    > will have to define a new array for indirect indexing.
    >
    > Sorting without assigning indices is nonsense.
    >
    > > An array isn't sorted any way. Neither are its properties,
    > > necessarily. You can order the properties numerically, but that's not
    > > a property of the array.

    >
    > an array in sense strictior has no properties, that is object behavour,
    > and a javascript array has object behavour.
    >
    > >> This should work too, but I get an upsifde down result:

    >
    > >> var indexArr = indexArr.sort(sortFunct);

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

    >
    > > That won't work. The comparison function should return positive, zero
    > > or negative depending on the relative order of its argument. This
    > > function only returns a boolean value, which converts to either 0 or 1
    > > as a number, so it can't represent that b is less than a.

    >
    > Ah yes, true.
    >
    > > A comparison function that does work would be

    >
    > > function cmp(a, b) {  // Generic comparison function compatible with
    > > '<'
    > >   return (b < a) - (a < b);  // Obscure, but it works.
    > >}

    >
    > > // Reversing comparison:
    > > function sortFunct(a, b) {
    > >   return cmp(theArr, theArr[a]);
    > >}

    >
    > > Then you can do:
    > >  indexArr.sort(sortFunct);

    >
    > Yes!
    >
    > > (No need to assign it back to indexArr. The sort function updates the
    > > array in-place).

    >
    > Sorry? No, not according to the OP's wishes.
    >
    > So try this, OP:
    >
    > =============================================
    > <script type='text/javascript'>
    >
    > var theArr = [];
    > var indexArr = [];
    > var temp;
    >
    > theArr[2] = 'Netherland';
    > theArr[5] = 'Africa';
    > theArr[3] = 'India';
    > theArr[4] = 'USA';
    >
    > // fill indexArr with pointers, skipping not assigned values
    > var y = 0;
    > for(var x = 0; x < theArr.length; x++) {
    >   if (theArr[x] != undefined) {
    >     indexArr[y++] = x;
    >   };
    >
    > };
    >
    > // indirect index sort
    > var indexArr = indexArr.sort(sortFunct);
    >
    > // inner sort function declaration
    > function sortFunct(a,b) {
    >   return (theArr[a] > theArr)? 1:(theArr[a] < theArr)?-1:0;
    >
    > }
    >
    > // show
    > for(x = 0; x < indexArr.length; x++) {
    >   document.write(x + ': index: ' + indexArr[x]);
    >   document.write(' value: ' + theArr[indexArr[x]] + '<br>');
    >
    > };
    >
    > </script>
    > ==========================================
    >
    > Now the indexArr has values
    > pointing to the theArr indices
    > in the order of the sorted theArr.
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)


    Thanks Evertjan. Great work :)
    The results are as per my requirement .
     
    Rahul, Jun 10, 2009
    #14
  15. Rahul

    Rahul Guest

    On Jun 9, 8:39 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > Rahul wrote:
    > > Thomas 'PointedEars' Lahn wrote:
    > >> [snipped rest of full quote]
    > >> The point of sorting is that the key for a value, or the value for a key,
    > >> *changes* according to the sort order.
    > >> [...]

    >
    > The last time you have been asked to quote properly according to what also
    > the FAQ of this newsgroup recommends has already passed.  FOAD.
    >
    > >  Explanation seems to be pure rubbish.

    >
    > As did your question.
    >
    > *PLONK*
    >
    > PointedEars


    PointedEars you are sick . never reply me in the future .
    I have also reported your case to Cyber Crime , they are trapping your
    IP better
    you run from here ..
     
    Rahul, Jun 10, 2009
    #15
  16. "Evertjan." <> writes:

    > Lasse Reichstein Nielsen wrote on 09 jun 2009 in comp.lang.javascript:
    >
    >> "Evertjan." <> writes:
    >>
    >>> [Garbage in, garbage out, remember.]
    >>>
    >>> states_array['2'] = '';
    >>>
    >>> 1 That is not a javascript array, because an array has a numeric
    >>> index.

    >>
    >> Actually not.
    >> All object properties are strings, not numbers, including array
    >> indices. It's just that array indices exactly the result of doing
    >> toString on values in the range 0..2^32-1.

    >
    > But an array is not just an object, the numeric indices give special end
    > extra seriality to the numeric behavour of the array.


    That's a meaning that is not intrinsic in the array itself.
    In other languages, e.g., C, an array is really a sequence of values,
    with memory location laid out in order.
    Javascript does not specify the memory model. The elements of an array
    doesn't need to have any inherent sequential ordering. You can order
    the indices, but you can also order the property names of
    var r = {rax : 0, rcx: 1, rdx: 2, rbx: 3};
    You might think that there is a sequence going: rax, rbx, rcx, rdx, but
    that's all in your perception. Likewise the numeric ordering of array
    indices isn't inherent in the array, nor in the language, but merely
    in your perception of it.

    > That an array also exhibit "normal" object behavour is a pity or an
    > advantage in Javascript, depending on your mood.
    >
    >>> 2 An array is sorted by it's index.

    >>
    >> Hmm. Either it's a tautology, or it's just meaningless.

    >
    > You should make up your mind.


    Ok, it's a tautology.
    An array "being sorted" means that there is an ordering relation such
    that larger indices have values that are "greater" than the values
    of lower indices.
    I.e., an array, a, is sorted, if
    i,j being array indices: i < j => a less-than-or-equal a[j]
    where less-than-or-equal is the ordering relation.

    Being ordered by its index (assuming numerically) is a tautology in
    that case: It's sorted if i < j implies i <= j.

    > It may be a tautology in the sense that you alreaduy knew it was so, and
    > nit different, but wether or not it is not meaningless.
    >
    > If you sort an array, the indices get new values, so that the order of
    > indices remains the same amd the values are sorted, bubbled or otherwise.


    Indices have no order, except what you percieve. Sure, it's traditional
    to see non-negative integers as ordered by value, but that has nothing
    to do with the Array.

    > If you want to sort an array without changing the inex-value pairing, you
    > will have to define a new array for indirect indexing.


    Perfectly good approach.

    > Sorting without assigning indices is nonsense.


    Assigning indices to values or values to indices, both make sense.
    Being sorted is when a relation on indices and a relation on values
    coincide.

    >> An array isn't sorted any way. Neither are its properties,
    >> necessarily. You can order the properties numerically, but that's not
    >> a property of the array.

    >
    > an array in sense strictior has no properties, that is object behavour,
    > and a javascript array has object behavour.


    And array elements are also object properties. In fact, they are
    nothing but object properties. The thing that makes an array special
    is the "length" property, and nothing but.


    >> (No need to assign it back to indexArr. The sort function updates the
    >> array in-place).

    >
    > Sorry? No, not according to the OP's wishes.
    >


    I was referring to this line only:

    > // indirect index sort
    > var indexArr = indexArr.sort(sortFunct);


    The assignment is unnecessary. The line would have exactly the same effect
    if it was just:
    indexArr.sort(sortFunct);
    The sort function updates the indexArr array destructively, and then
    returns the array again. Reassigning indexArr to the same value it already
    had does not make a difference.
    (And this also avoids the problem that indexArr was already declared
    as a variable earlier).


    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
     
    Lasse Reichstein Nielsen, Jun 10, 2009
    #16
  17. Rahul

    Evertjan. Guest

    Lasse Reichstein Nielsen wrote on 10 jun 2009 in comp.lang.javascript:

    > "Evertjan." <> writes:


    [..]

    >> But an array is not just an object, the numeric indices give special
    >> end extra seriality to the numeric behavour of the array.

    >
    > That's a meaning that is not intrinsic in the array itself.
    > In other languages, e.g., C, an array is really a sequence of values,
    > with memory location laid out in order.
    > Javascript does not specify the memory model. The elements of an array
    > doesn't need to have any inherent sequential ordering. You can order
    > the indices, but you can also order the property names of
    > var r = {rax : 0, rcx: 1, rdx: 2, rbx: 3};
    > You might think that there is a sequence going: rax, rbx, rcx, rdx,
    > but that's all in your perception. Likewise the numeric ordering of
    > array indices isn't inherent in the array, nor in the language, but
    > merely in your perception of it.
    >
    >> That an array also exhibit "normal" object behavour is a pity or an
    >> advantage in Javascript, depending on your mood.
    >>
    >>>> 2 An array is sorted by it's index.
    >>>
    >>> Hmm. Either it's a tautology, or it's just meaningless.

    >>
    >> You should make up your mind.

    >
    > Ok, it's a tautology.


    Then any definition is a tautology,
    if you knew the definition beforehand as being sensible and apt.


    > An array "being sorted" means that there is an ordering relation such
    > that larger indices have values that are "greater" than the values
    > of lower indices.
    > I.e., an array, a, is sorted, if
    > i,j being array indices: i < j => a less-than-or-equal a[j]
    > where less-than-or-equal is the ordering relation.
    >
    > Being ordered by its index (assuming numerically) is a tautology in
    > that case: It's sorted if i < j implies i <= j.


    Do you have something against tautologies? ;-))

    In that definition of tautologies, tautologies are a good base for
    teaching definitions to people that do not have such knowledge yet.

    Should the idea that the definition of tautology
    is a tautology in itself, be called a "metatautology"?
    ... and the sciense of such called "metatautologica"?
    ...or "metatautologonomia"?

    ;-)))

    >> It may be a tautology in the sense that you alreaduy knew it was so,
    >> and nit different, but wether or not it is not meaningless.
    >>
    >> If you sort an array, the indices get new values, so that the order
    >> of indices remains the same amd the values are sorted, bubbled or
    >> otherwise.

    >
    > Indices have no order, except what you percieve. Sure, it's
    > traditional to see non-negative integers as ordered by value, but that
    > has nothing to do with the Array.


    Oh yes it has.

    The order is in the order of numeric indices, that really is a definition
    of an array. [A tautology you would say perhaps].

    The idea that not all members should be defined below the top one,
    is a good idea, and that other languages do not encompass such is their
    problem. It is not detrimental to the array idea.


    >> If you want to sort an array without changing the inex-value pairing,
    >> you will have to define a new array for indirect indexing.

    >
    > Perfectly good approach.


    Thanks

    >> Sorting without assigning indices is nonsense.

    >
    > Assigning indices to values or values to indices, both make sense.
    > Being sorted is when a relation on indices and a relation on values
    > coincide.


    Depending on the meaning of "order of values", you could orde them
    defining that all stringvalues starting with a consonant come first.

    >>> An array isn't sorted any way. Neither are its properties,
    >>> necessarily. You can order the properties numerically, but that's
    >>> not a property of the array.


    It is the essence of an array, otherwise it is not an array,
    but just an object with properties.


    >> an array in sense strictior has no properties, that is object
    >> behavour, and a javascript array has object behavour.

    >
    > And array elements are also object properties. In fact, they are
    > nothing but object properties. The thing that makes an array special
    > is the "length" property, and nothing but.


    No, that would be the same as saying a script is just a string.

    These bits have a special meaning in the sense that in a certain
    environment, they can influence a rendering programme to do what you
    want, and that you can read such string understanding what it should do.

    The same is with an array, It will do special things because that is how
    it is percieved. All members of an array have a numerical index, members
    without such numerical index are not part of the array, though they could
    be part of such array in it's object form.

    Arrays cannot have two members with te same index, or members with a
    negative or noninteger value, object can have such properties.


    >>> (No need to assign it back to indexArr. The sort function updates
    >>> the array in-place).

    >>
    >> Sorry? No, not according to the OP's wishes.
    >>

    >
    > I was referring to this line only:
    >
    >> // indirect index sort
    >> var indexArr = indexArr.sort(sortFunct);

    > The assignment is unnecessary. The line would have exactly the same
    > effect if it was just:
    > indexArr.sort(sortFunct);


    Yesss!

    > The sort function updates the indexArr array destructively, and then
    > returns the array again. Reassigning indexArr to the same value it
    > already had does not make a difference.
    > (And this also avoids the problem that indexArr was already declared
    > as a variable earlier).


    I knew that, years ago, but seem to have forgotten that.
    Thanks for helping me remember.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jun 10, 2009
    #17
  18. Rahul meinte:

    > PointedEars you are sick . never reply me in the future .


    How do you intend to enforce that?

    > I have also reported your case to Cyber Crime , they are trapping your
    > IP better
    > you run from here ..


    "Cyber Crime"? What's that? (And why would anyone need to trap his IP?
    One can easily look up TPL's whereabouts.) Rahul you're utterly pathetic.



    --
    http://www.gregorkofler.com
    http://web.gregorkofler.com - vxJS, a JS lib in progress
     
    Gregor Kofler, Jun 11, 2009
    #18
  19. Gregor Kofler wrote:
    > Rahul meinte:
    >> PointedEars you are sick . never reply me in the future .

    >
    > How do you intend to enforce that?


    Posting enough clueless nonsense so that he gets into my killfile and I will
    never see one of his postings again (except in quotes). And he succeeded,
    congratulations.

    >> I have also reported your case to Cyber Crime , they are trapping your
    >> IP better
    >> you run from here ..

    >
    > "Cyber Crime"? What's that?


    Probably <http://cybercrime.planetindia.net/>. Never mind that they don't
    listen to people complaining about other people that they simply don't like.

    > (And why would anyone need to trap his IP? One can easily look up TPL's
    > whereabouts.)


    Well, there are enough people around the world with both the same first and
    last name ;-)

    > Rahul you're utterly pathetic.


    He's a luser, plain and simple.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Jun 11, 2009
    #19
  20. Rahul

    Rahul Guest

    On Jun 11, 11:37 pm, Gregor Kofler <> wrote:
    > Rahul meinte:
    >
    > > PointedEars you are sick . never reply me in the future .

    >
    > How do you intend to enforce that?
    >
    > > I have also reported your case to Cyber Crime , they are trapping your
    > > IP  better
    > > you run from here ..

    >
    > "Cyber Crime"? What's that? (And why would anyone need to trap his IP?
    > One can easily look up TPL's whereabouts.) Rahul you're utterly pathetic.
    >
    > --http://www.gregorkofler.comhttp://web.gregorkofler.com- vxJS, a JS lib in progress


    First you go through the entire post spl 'where pointed ears started
    behaving like a assbite '.
    All this has been started from his side , he's simply raising fingers
    while replying /posting ,even though his reply is purely meaning less.
     
    Rahul, Jun 12, 2009
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Navin
    Replies:
    1
    Views:
    728
    Ken Schaefer
    Sep 9, 2003
  2. thunk
    Replies:
    1
    Views:
    330
    thunk
    Mar 30, 2010
  3. thunk
    Replies:
    0
    Views:
    509
    thunk
    Apr 1, 2010
  4. thunk
    Replies:
    14
    Views:
    643
    thunk
    Apr 3, 2010
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    321
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page