www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: accessing orig. counter in sorted array

  1. #1
    Join Date
    Jul 2010
    Posts
    17

    accessing orig. counter in sorted array

    Newbie question:

    Okay, so I've sorted a numerical array and the minimum is at the top of the heap with [0]. How do I keep track of the original [?] so that I can access the associated parameters?

  2. #2
    Join Date
    Oct 2006
    Posts
    939
    Make another variable equal to the unsorted array

    HTML Code:
    <SCRIPT type="text/javascript">
    var arr=[2,6,8,3]
    var brr=[]; for(i=0;i<arr.length;i++){brr[i]=arr[i];}
    arr=arr.sort(function(a,b){return parseInt(a) - parseInt(b)});
    document.write(arr+'<br>'+brr)
    </SCRIPT>

  3. #3
    Join Date
    Jul 2010
    Posts
    17
    Quote Originally Posted by justinbarneskin View Post
    Make another variable equal to the unsorted array

    HTML Code:
    <SCRIPT type="text/javascript">
    var arr=[2,6,8,3]
    var brr=[]; for(i=0;i<arr.length;i++){brr[i]=arr[i];}
    arr=arr.sort(function(a,b){return parseInt(a) - parseInt(b)});
    document.write(arr+'<br>'+brr)
    </SCRIPT>
    I evidently didn't do a good job of presenting my problem.

    Using your example, the original array is:
    arr[0]=2
    arr[1]=6
    arr[2]=8
    arr[3]=3

    There exists another array:
    ARR[0]="Chevrolet"
    ARR[1]="Ford"
    ARR[2]="Mazda"
    ARR[3]="Toyota"

    After sorting arr() in descending order:

    arr[0]=8

    But, I want to access the parameter in the second array (Mazda) which had been associated with "8."

    How do I do it?

  4. #4
    Join Date
    Aug 2007
    Posts
    3,767
    You'd probably be better off like this.
    Code:
    var arr = [[2,"Chev"],[6,"Ford"],[8,"Mazda"],[3,"Toyota"]];
    arr.sort(function(a,b){return parseInt(a[0]) - parseInt(b[0])});
    With Justin's solution, you'd have to iterate through brr for each value in arr, and take the corresponding value from crr, your list of cars.
    Great wit and madness are near allied, and fine a line their bounds divide.

  5. #5
    Join Date
    Jul 2010
    Posts
    17
    Getting close, but still not there.

    I want to be able to pull out a car and use it elsewhere.

    Something like "document.write(arr[1][0]);," for instance, doesn't work.

  6. #6
    Join Date
    Aug 2007
    Posts
    3,767
    From which array. The original order or the sorted order? The more explicit you are in what you are trying to do, the easier it is to help you.

    And if you want the car name it would be arr[1][1] not arr[1][0].
    Great wit and madness are near allied, and fine a line their bounds divide.

  7. #7
    Join Date
    Jul 2010
    Posts
    17
    Quote Originally Posted by Declan1991 View Post
    From which array. The original order or the sorted order? The more explicit you are in what you are trying to do, the easier it is to help you.

    And if you want the car name it would be arr[1][1] not arr[1][0].
    I want the association which existed in the original array.

    Disregard stupid question below. Values must be assigned before the next array is declared. This looks promising!

    Here's something...from one of the many javascript help sites...which would, I believe, solve my problem (if I can get it to work).

    var cube = new Array();
    cube[0] = new Array(); // This gives us a two dimensional array
    cube[0][0] = new Array(); // This gives us a three dimensional array
    cube[0][0][3] = "Hello";
    // This is how we use a three dimensional array

    and document.write(cube[0][0][3]) yields "Hello."

    But, as soon as I assign a value in the second array:

    var cube = new Array();
    cube[0] = new Array(); // This gives us a two dimensional array
    cube[0][0] = new Array(); // This gives us a three dimensional array

    cube[0][0]=2;

    cube[0][0][3] = "Hello";
    // This is how we use a three dimensional array

    document.write(cube[0][0][3]) yields "undefined."

    Doesn't seem to make sense. What good is a multidimensional array if you can only use the last array?
    Last edited by BillyShope; 07-09-2010 at 07:33 AM. Reason: added "Disregard" paragraph.

  8. #8
    Join Date
    Aug 2007
    Posts
    3,767
    What you are trying is making no sense. You assign a value to cube[0][0] and then you pretend like it's an array.
    Code:
    cube[0][0] = new Array(); // This gives us a three dimensional array
    cube[0][0]=2; // Which you overwrite
    
    Without knowing what problem you are trying to solve, it's impossible to help you. In general, it's far better to just outline your problem rather than asking how to code a specific solution.
    Great wit and madness are near allied, and fine a line their bounds divide.

  9. #9
    Join Date
    Jul 2010
    Posts
    17
    Quote Originally Posted by Declan1991 View Post
    What you are trying is making no sense. You assign a value to cube[0][0] and then you pretend like it's an array.
    Code:
    cube[0][0] = new Array(); // This gives us a three dimensional array
    cube[0][0]=2; // Which you overwrite
    
    Without knowing what problem you are trying to solve, it's impossible to help you. In general, it's far better to just outline your problem rather than asking how to code a specific solution.
    Yes, I realized later I was making that mistake.

    Okay, here's my problem: I'm working with a mechanism that has hundreds of different linkage arrangements. I have a target number and can calculate, for each linkage arrangement, just how far I am from the target. I want to give the user the 5 best linkage arrangements, or, the 5 linkage arrangements which deviate the least from the target. So, once I've sorted, I need to recall the linkage arrangements for each of the 5.

    Thank you.

  10. #10
    Join Date
    Aug 2007
    Posts
    3,767
    Then I would try something like this.
    Code:
    var arr = ["Chevrolet","Ford","Mazda","Toyota"];
    var newarr = [[],[],[],[],[]];
    var str = ["2,1,3,4","","","",""];
    for (var i = 0; i < str.length; i++) {
    var temp = str[i].split(",");
    for (var j = 0; j < str[i].length; j++) {
        newarr[i][j] = arr[temp[j]-1];
    }
    }
    // newarr[0] = ["Ford","Chevrolet","Mazda","Toyata"];
    // arr = original one
    I think that's more suitable?
    Great wit and madness are near allied, and fine a line their bounds divide.

  11. #11
    Join Date
    Jul 2010
    Posts
    17
    Don't fully understand it yet, but it appears to be the answer.

    Thanks a lot!

  12. #12
    Join Date
    Jul 2010
    Posts
    17
    Yours was indeed the solution to my initial problem, but it introduced a couple of other problems.

    First, I didn't immediately realize that I was...with your solution...sorting a string variable. I'll be working with numbers. I suppose I could multiply by a constant, round, and convert to a string, but that wouldn't help me with the second problem.

    As mentioned earlier, I won't be working with 4 cases, but with hundreds. I would prefer inputting to a simple numeric array with each iteration.

    Sorry to keep coming back, but I've put a lot of work into this spreadsheet...one of many which I provide free to hundreds of clients..., but it's of no value if I can't output the results.

    Thank you.
    Last edited by BillyShope; 07-10-2010 at 06:24 AM. Reason: added "...with your solution..."

  13. #13
    Join Date
    Aug 2007
    Posts
    3,767
    You mean this bit?
    Code:
    var str = ["2,1,3,4","","","",""];
    How do you want the data to be inputted? Where are these lists coming from? I presumed a comma separated list for some reason, but an array is almost the exact same, because I converted that list to an array anyway.
    Code:
    var str = [[2,1,3,4],[4,3,2,1],[],[],[]];
    for (var i = 0; i < str.length; i++) {
    // var temp = str[i].split(","); No need for this line anymore 
    And I think I mentioned that [] is just shorthand for an array.
    Code:
    var arr = new Array();
    arr[0] = 1;
    arr[1] = 2;
    Is exactly the same as this.
    Code:
    var arr = [1,2];
    And don't worry too much about typecasting in JavaScript. It will do it automatically for you most of the time. The only time you really have to worry is when adding two numbers in string format, because concatenation operator is the same as addition.
    Code:
    alert("1"+"2"); // = 12
    alert(1 + 2); // = 3
    Great wit and madness are near allied, and fine a line their bounds divide.

  14. #14
    Join Date
    Jul 2010
    Posts
    17
    I believe I've figured it out. And, when you see my solution, I'm sure you'll be saying, "Well, if that's all you needed." I know so little about Javascript that I'm embarrassed to come to a forum like this, but I was desperate. Sorry I couldn't explain my problem more accurately.

    arr=new Array();
    arr[0]=new Array();
    arr[0][0]=2;
    arr[0][1]=1;
    arr[0][2]=4;
    arr[0][3]=3;

    arr[1]=new Array();
    arr[1][0]=arr[0][0];
    arr[1][1]=arr[0][1];
    arr[1][2]=arr[0][2];
    arr[1][3]=arr[0][3];
    document.write(arr[0][0]+"<br>");
    arr[0].sort();
    document.write(arr[0][0]+"<br>"+arr[1][0]);

  15. #15
    Join Date
    May 2010
    Posts
    109
    The OP ignored the code I posted.
    Last edited by Westerly; 07-10-2010 at 09:32 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles