Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting an associative array 1

Status
Not open for further replies.

Deleted

Technical User
Jul 17, 2003
470
US
O.K. I have this code..

Code:
var capitals = new Array();

capitals("New Delhi") = "India";
capitals("Beijing") = "China";
capitals("London") = "Britain";
capitals("Paris") = "France";

var s_capitals = new Array("New Delhi", "Beijing", "London", "Paris");
s_capitals.sort();

for(i = 0; i < s_capitals.length; i++) {
	document.write("<p>" + s_capitals[i] + ": " + capitals[s_capitals[i]] + "</p>");
}

This code works on sorting an associative array. The only problem is if there are duplicates that need to be sorted (ex: "New Delhi", "Paris", "London", "Paris") it fails miserably. Has anyone messaed around with associative array sorting? Any way of getting this script to work with duplicates? Thanks for your time.

 
>[tt]capitals("New Delhi") = "India";[/tt]
etc.
Did you try it over? It won't work unless you reference the key properly.
[tt]capitals[red][[/red]"New Delhi"[red]][/red] = "India";[/tt]

 
If you want to sort the KEYS of an associative array, here is one way to do it:
Code:
var sortedKeys = new Array();
function sortArray() {
  var n = 0;
  for (var x in capitals) {
    sortedKeys[n++] = x;
  }
  sortedKeys.sort();
}
function showKeys() {
  for (var x in sortedKeys) {
    output += sortedKeys[x] + "<br>";
  }
  document.getElementById("output").innerHTML = output;
}

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
What do mean it "fails miserably"? I tried your example and it did exactly what I expected it to do. What are you expecting it to do?
Code:
<script>
var capitals = new Array();

capitals["New Delhi"] = "India";
capitals["Beijing"] = "China";
capitals["London"] = "Britain";
capitals["Paris"] = "France";

var s_capitals = new Array("New Delhi", "Paris", "London", "Paris");
s_capitals.sort();

for(i = 0; i < s_capitals.length; i++) {
    document.write("<p>" + s_capitals[i] + ": " + capitals[s_capitals[i]] + "</p>");
}

</script>
Returns:
Code:
London: Britain

New Delhi: India

Paris: France

Paris: France

Adam
 
Thanks for the replies.. As for adam0101 reply. I didn't explain myself correctly.

capitals("New Delhi") = "India";
capitals("Beijing") = "China";
capitals("London") = "Britain";
capitals("Paris") = "France";
capitals("Paris") = "Texas";

This will fail on the duplicates with the script I provided.

 
tsdragon I took the code you provided and came up with the same problem. It still fails on duplicates like the example below. Any suggestions around this?

Code:
<html>
<head>

<script language="javascript">
<!--//
var sortedKeys = new Array();

function sortArray() {
	var capitals = new Array();
	capitals["New Delhi"] = "India";
	capitals["Beijing"] = "China";
	capitals["London"] = "Britain";
	capitals["Paris"] = "France";
	capitals["Paris"] = "Texas";

	var n = 0;
	for (var x in capitals) {
		sortedKeys[n++] = x;
	}
	sortedKeys.sort();

	showKeys()
}

function showKeys() {
	var output = "";
	for (var x in sortedKeys) {
		output += sortedKeys[x] + "<br>";
	}
	document.getElementById("output").innerHTML = output;
}
//-->
</script>

</head>
<body onLoad="sortArray()"><div id="output"></div></body>
</html>

 
Since Austin is the REAL capital of Texas, what is the REAL code you're having problems with?

Lee
 
You'll never get it to work. The thing with associative arrays is that the keys must be unique.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Instead of an associative array, I'd use an array of objects.

Code:
<html>
<head>
<script language="javascript">

var capitals = new Array();

function sortArray() {
    addCapital("New Delhi","India");
    addCapital("Beijing","China");
    addCapital("London","Britain");
    addCapital("Paris","France");
    addCapital("Paris","Texas");

    capitals.sort(customSort);

    showKeys();
}
function addCapital(city,country){
    capitals[capitals.length] = new Capital(city,country);
}

[green]//Object to hold capital and country[/green]
Capital = function(city,country){
    this.city = city;
    this.country = country;
}

[green]//Function to sort our custom objects[/green]
function customSort(a,b){
    if(a.city > b.city){return 1}
    if(a.city < b.city){return -1}
    return 0
}

function showKeys() {
    var output = "";
    for (var i=0;i<capitals.length;i++) {
        output += capitals[i].city + "<br>";
    }
    document.getElementById("output").innerHTML = output;
}
//-->
</script>

</head>
<body onLoad="sortArray()"><div id="output"></div></body>
</html>

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top