sorting - Sort a sublist prolog -


i have list in prolog:

[[13,audi a3,11.11.2011,75000,berlina,audi,12100,verde pisello,[4wd],3.0000133333333334],[11,santafe,11.11.2011,80000,fuoristrada,audi,2232232,verde pisello,[metalizzata,sedile in pelle,4wd],7.0000125]]|

i want sort list last value of sublist, example want have result:

[[11,santafe,11.11.2011,80000,fuoristrada,audi,2232232,verde pisello,[metalizzata,sedile in pelle,4wd],7.0000125],[13,audi a3,11.11.2011,75000,berlina,audi,12100,verde pisello,[4wd],3.0000133333333334]]

predsort it's friend. sort easy, sell audi verde pisello remain very, hard...

sort_on_last(list, sorted) :-     predsort(compare_last, list, sorted).  compare_last(r, x, y) :-     last(x, xl),     last(y, yl),     compare(r, xl, yl). 

to try it:

test :- sort_on_last(         [[11,santafe,'11.11.2011',80000,fuoristrada,audi,2232232,'verde pisello',['metalizzata','sedile in pelle','4wd'],7.0000125],          [13,'audi a3','11.11.2011',75000,berlina,audi,12100,'verde pisello',['4wd'],3.0000133333333334]         ], s),     maplist(writeln, s). ?- test. [13,audi a3,11.11.2011,75000,berlina,audi,12100,verde pisello,[4wd],3.0000133333333334] [11,santafe,11.11.2011,80000,fuoristrada,audi,2232232,verde pisello,[metalizzata,sedile in pelle,4wd],7.0000125] true. 

a particularity of predsort/3: acts sort/2, remove duplicates. avoid problem, compare_last/3 can changed, avoiding return =, in way:

compare_last(r, x, y) :-     last(x, xl),     last(y, yl),     ( xl < yl -> r = (<) ; r = (>) ). 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -