I have a list which includes several lists inside. Is there any way that I can sort the lists alphabetly based on the first part of each list (signal name)?
list("V105A_ymax_v9overshoot_1" float(0.9975) float(1.1025) "[V]" )list("V105A_riseTime_v9ramptime_1" float(300u) float(2m) "[s]" )list("V5A_DS3_delay10_v5a_on_1" float(0.0) float(500u) "[s]" )list("V33A_DSW_peakToPeak_v4vripple_1" float(0.0) float(50.0m) "[V]" )list("V5A_DS3_ymin_v2pgoodincr_1" float(4.65) float(4.85) "[V]" )list("DPWROK_delay90_pwrok_delay_1" float(10m) float(15m) "[s]" )list("V33A_DSW_average_v4voutavg_1" float(3.135) float(3.465) "[V]" )list("V105A_ymin_v9pgoodincr_1" float(0.965) float(1.03) "[V]" )
The simplest way to do this is to use sortcar() with your example. Assuming myList contains the list:
That outputs this:
(("DPWROK_delay90_pwrok_delay_1" 0.01 0.015 "[s]") ("V105A_riseTime_v9ramptime_1" 0.0003 0.002 "[s]") ("V105A_ymax_v9overshoot_1" 0.9975 1.1025 "[V]") ("V105A_ymin_v9pgoodincr_1" 0.965 1.03 "[V]") ("V33A_DSW_average_v4voutavg_1" 3.135 3.465 "[V]") ("V33A_DSW_peakToPeak_v4vripple_1" 0.0 0.05 "[V]") ("V5A_DS3_delay10_v5a_on_1" 0.0 0.0005 "[s]") ("V5A_DS3_ymin_v2pgoodincr_1" 4.65 4.85 "[V]"))
In the absence of sortcar, you could do:
myList=sort(myList lambda((a b) alphalessp(car(a) car(b))))
In other words, the second argument to sort is a function that compares two values and returns t if the first is "less" than the second. You can then do whatever comparison you like - here I'm using alphalessp on the car of each entry, which is your signal name here. I could equally well sort on the second entry in each list (the numbers) by doing:
myList=sort(myList lambda((a b) cadr(a)<cadr(b)))
And so on.
In reply to Andrew Beckett:
never mind. I think I can build the list twice.
Thanks for the tips.
In reply to mbchang:
To respond to your last question (that's now disappeared) - how about sorting everything in the list except for the first element? In that case you could sort the cdr of the list, that is to say, everything in the 'tail' of the list, after taking the first item (the 'head') away. Beware that "sort" is a destructive list operator, so you must save the return result of the sort function since it modifies the list in place.
Hopefully this helps you?
In reply to skillUser:
This could probalby do it.
Andrew wasn't the intial code from you? Can't remeber.
In reply to berndfi:
I agree (the code wasn't from me though - I don't believe in having the type of variables encoded in the names as it makes them hard to read, so it wasn't my code).