473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

sorting associative arrays

verbatim
array is generated by user inputing a random word list that is then organized by word length:

[#] = length of each word in each array

if i have an array ordered as such:

Expand|Select|Wrap|Line Numbers
  1. wordList[9] = ["associate", "documents" ]
  2. wordList[5] = ["green", "about", "money" ]
  3. wordList[3] = ["dog", "cat", "top", "big", "vat" ]
  4. wordList[6] = ["floats" ]
  5.  
  6. how can i get these desired ordering results:
  7.  
  8. wordList[3] = ["dog", "cat", "top", "big", "vat" ]
  9. wordList[5] = ["green", "about", "money" ]
  10. wordList[6] = ["floats" ]
  11. wordList[9] = ["associate", "documents" ]
  12.  
  13. my first attempt to reorder, rendered the following:
  14.  
  15. wordList[9] = ["dog", "cat", "top", "big", "vat" ]
  16. wordList[5] = ["green", "about", "money" ]
  17. wordList[3] = ["floats" ]
  18. wordList[6] = ["associate", "documents" ]
  19.  
correctly re-ordered each array, but not the [#] associated with it.
Oct 28 '07 #1
1 1470
Logician
210 100+
Expand|Select|Wrap|Line Numbers
  1. wordList[9] = ["associate", "documents" ]
  2. wordList[5] = ["green", "about", "money" ]
  3. wordList[3] = ["dog", "cat", "top", "big", "vat" ]
  4. wordList[6] = ["floats" ]
You are not using associative array syntax.
You have a partially-populated two dimensional subscripted array.
On the assumption that all unmentioned array elements are undefined, and also assuming that the strings in each sub array will all be of the same length, you can copy just the defined elements of wordList to a new array, sort the array using a suitable comparator, then overwrite just the defined elements of wordList with all the elements of the sorted temporary array.

I tried it with no problems.
Oct 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
7
by: Karin Jensen | last post by:
Hi I am running a PHP program that connects to an Access 2000 database via ODBC: $results = odbc_exec($connection_id, $sql_select); Is it possible to sort the contents of $results? I wish to...
11
by: Paul Lautman | last post by:
I'm having some trouble understanding what is happening with some array sorting functions. In all cases, my compare function is: function compare($x, $y) { if ( $x == $y ) return 0; else if (...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
4
by: mk834tt | last post by:
When you use an object as an assc array as in: var aa = new Array(); aa = 2.50; aa = 3.50; .... Is there a way to determine the number of entries? So far I count this way: for (var x in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.