HowTo sort an Array not vor Names but for Values 
Author Message
 HowTo sort an Array not vor Names but for Values

Hey and hello,

I just wonder if someone has a nice statement ready if you want to
sort an array for the values not for the names...

[lsort -ascii [array names an_array]]

does this nice for the indices names. However I would like to have a sorted
array
where the order of the array elements depends on the sorting of their values
...

Thank's ...

--
********************************************************************
** Meng Engineering        Telefon    056 222 44 10               **
** Markus Meng             Natel      079 230 93 86               **
** Bruggerstr. 21          Telefax    056 222 44 10               **

********************************************************************
** Programmers don't die, they just GOSUB without RETURN.         **
********************************************************************



Sun, 07 Apr 2002 03:00:00 GMT  
 HowTo sort an Array not vor Names but for Values

Quote:

> Hey and hello,

> I just wonder if someone has a nice statement ready if you want to
> sort an array for the values not for the names...

> [lsort -ascii [array names an_array]]

> does this nice for the indices names. However I would like to have a sorted
> array
> where the order of the array elements depends on the sorting of their values
> ...

An (associative) array never has an order, but you can hold a list as
ordered index to that array, e.g.,

set temp {}
foreach {key value} [array get myarr] {
    lappend temp [list $key $value]

Quote:
}

lsort -ascii -index 1 $temp
set result {}
foreach pair $temp {
    lappend result [lindex $pair 0]
Quote:
}

# ordered:
foreach key $result {
   # what do you want to do today with myarr($key) ?

Quote:
}

Cheers,

- Andrej.

------------------------------------------------------------------------

Netcetera AG, 8040 Zuerich    phone +41 1 247 79 05  fax +41 1 247 70 75



Sun, 07 Apr 2002 03:00:00 GMT  
 HowTo sort an Array not vor Names but for Values

Quote:

> I just wonder if someone has a nice statement ready if you want to
> sort an array for the values not for the names...
> [lsort -ascii [array names an_array]]

see manual for  lsort -command ...

you can specify a procedure like this:
proc com {arr x y} { upvar 1 $arr a
    return [compare $a($x) $a($y)]

Quote:
}

lsort ... -command {com an_array} ...

(use some appropriate compare-command rather than "compare")



Sun, 07 Apr 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. HOWTO: reference elements of an array if array name is in a variable

2. java array return value not array in tcl

3. Sorting an array based on values

4. Sort a 2 dimensional array by a value

5. Copying one array value into subsequent array values

6. field value as array name?

7. Array = Hash.sort ... but sometimes not

8. MS Fort - DATA not array-element name?

9. Array#sort! returns nil when array empty

10. mini-howto: sorting

11. DOS File Lookup - just the file name, not the path AND file name

12. Index Values of an Array inside a Cluster inside an Array

 

 
Powered by phpBB® Forum Software