Thread: Sorting

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    56
    Rep Power
    28

    Sorting


    I have an array:
    1=871561,2=198298,3=8361411,4=49356,5=47642....
    I shortened it a lot for this post, there are 639 items in the array. I am attempting to set up a script to auto complete filling in a sort form where the value entered on the form controls where the page displays the item. The lowest number displays at the top of the page, then the second lowest, and so on. In the case of the array above, I want the items sorted exactly the way the page would sort them if I entered the numbers into the form for each item, but the highest number that can be entered is 32,767 and all of my numbers above are higher than that. There can be numbers as low as 1, and the sky is the limit for the highest possible value.

    I want to build a second array based on the values in the array above something like this:

    1=4,2=3,3=5,4=2,5=1
    then enter those values on the page to achieve my goal. So basically the lowest number in the first array becomes a 1 in the second, then the next lowest becomes a 2 and so-on.

    I don't know what this is called, or how to begin searching for help on this topic using Google, so even if you can only tell me what to look for that would be most appreciated. I just don't know where to begin.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0
    Originally Posted by efilnikufecin
    I have an array...I want to build a second array based on the values in the array above...
    What you're trying to achieve is actually built in to the Array object, it's called sort(). You can read more here (sorry, I can't post full links yet): developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort

    In effect, though, the relevant code for your implementation would be as follows:

    Code:
    var pageDisplayIndex = [871561,198298,8361411,...];
    var secondArray = pageDisplayIndex.sort(function(a,b){return a-b});
    This will sort pageDisplayIndex in ascending order, then store that in a second array. If you wanted to sort in descending order, you'd just return b-a.

    The same can be done for alphabetic array values, as well. Hope this helps.

    - Null
    Last edited by Kravvitz; February 12th, 2013 at 12:53 PM. Reason: replaced the link to w3schools
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    56
    Rep Power
    28
    If I sort using that method, my array indexes will be thrown off.

    Item #1 has an index of 1 in the array, and a value of 871561
    Item #2 has an index of 2 in the array, and a value of 198298
    Item #3 has an index of 3 in the array, and a value of 8361411
    Item #4 has an index of 4 in the array, and a value of 49356
    Item #5 has an index of 5 in the array, and a value of 47642

    using that method:
    Item #1 would have an index of 4 in the array, and a value of 871561
    Item #2 would have an index of 3 in the array, and a value of 198298
    Item #3 would have an index of 5 in the array, and a value of 8361411
    Item #4 would have an index of 2 in the array, and a value of 49356
    Item #5 would have an index of 1 in the array, and a value of 47642

    I would then attempt to access item number 1 in the array, and would find the value for item number 5 instead.

    I am using Jquery, and this is how I fill the form in:
    Code:
    			$('input[name="sort[]"]').each(function ()
    				{
    				$(this).attr('size', '20');
    				$(this).attr('maxlength', '20');
    				curItem = $(this).parent().siblings('input[type="hidden"]').attr('value');
    				$(this).attr('value', quantexisting[curItem]);
    				});
    quantexisting is the array the values are stored in, and that information is picked up on the page before, and stored for use on this page.

    What I need is to change the value of the lowest number stored in the array to a 1, the second lowest to a 2, the third lowest to a 3 and so on so that my values are lower than 32,767 and it can be stored in a 16 bit integer on the web sites database.


    A quick search on Google turned up Array.indexOf() which apparently doesn't work in some versions of Internet Explorer, but since I am working on a Greasemonkey script to fill in form data my script will likely never find it's way into Internet Explorer. I can use the original array to find the value of item number 1 "871561" and then find it's index in the second array "4" and use that value in the form field. I will begin work on it, but this method seems kind of cobbled, and I hope someone can find a better solution.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0
    Originally Posted by efilnikufecin
    If I sort using that method, my array indexes will be thrown off.
    I see what you're trying to achieve now. What you need to do is take the value of each item in your array and compare it to every other value. If it's greater, you increment a counter X, if it's equal or less than, do nothing. Then, at the end of this procedure, return X. The order of the array will be preserved, but you will have transformed each element into a more usable number.

    Using the parameters you defined, I have created a jsFiddle that does this: jsfiddle.net/zxdge/

    It first creates 639 random numbers from 1 through 99999, puts them in an array, and then sorts them out according to your specification.

    The code for this action is:

    javascript Code:
    function filterSort(array) {
        for ( var i = 0; i < array.length; i++ ) {
            var findValue = 1; var thisArrayValue = array[i];
            for ( var j = 0; j < array.length; j++ ) {
                if ( thisArrayValue > array[j] ) { findValue++; }
            }
            return findValue;
        }
    }


    This may be somewhat cobbled as well, and could probably be improved, but hopefully it's useful.

    - Null

IMN logo majestic logo threadwatch logo seochat tools logo