JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 12th, 2013, 12:34 AM
efilnikufecin efilnikufecin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 56 efilnikufecin User rank is Sergeant Major (2000 - 5000 Reputation Level)efilnikufecin User rank is Sergeant Major (2000 - 5000 Reputation Level)efilnikufecin User rank is Sergeant Major (2000 - 5000 Reputation Level)efilnikufecin User rank is Sergeant Major (2000 - 5000 Reputation Level)efilnikufecin User rank is Sergeant Major (2000 - 5000 Reputation Level)efilnikufecin User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 4 h 11 m 54 sec
Reputation Power: 27
Sorting

I have an array:
Quote:
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:

Quote:
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.

Reply With Quote
  #2  
Old February 12th, 2013, 11:14 AM
null.if.ied null.if.ied is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 26 null.if.ied User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 15 m
Reputation Power: 0
Quote:
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 11:53 AM. Reason: replaced the link to w3schools

Reply With Quote
  #3  
Old February 12th, 2013, 01:48 PM
efilnikufecin efilnikufecin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 56 efilnikufecin User rank is Sergeant Major (2000 - 5000 Reputation Level)efilnikufecin User rank is Sergeant Major (2000 - 5000 Reputation Level)efilnikufecin User rank is Sergeant Major (2000 - 5000 Reputation Level)efilnikufecin User rank is Sergeant Major (2000 - 5000 Reputation Level)efilnikufecin User rank is Sergeant Major (2000 - 5000 Reputation Level)efilnikufecin User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 4 h 11 m 54 sec
Reputation Power: 27
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.

Reply With Quote
  #4  
Old February 12th, 2013, 02:27 PM
null.if.ied null.if.ied is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 26 null.if.ied User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 15 m
Reputation Power: 0
Quote:
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:
Original - javascript Code
  1. function filterSort(array) {
  2.     for ( var i = 0; i < array.length; i++ ) {
  3.         var findValue = 1; var thisArrayValue = array[i];
  4.         for ( var j = 0; j < array.length; j++ ) {
  5.             if ( thisArrayValue > array[j] ) { findValue++; }
  6.         }
  7.         return findValue;
  8.     }
  9. }


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

- Null

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Sorting

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap