[Seed7-users] Sorting an array of array with a custom compare function
Interpreter and compiler for the Seed7 programming language.
Brought to you by:
thomas_mertes
From: Stéphane G. <ste...@wa...> - 2018-12-06 14:56:14
|
Hello, I would like to sort a table of rows, depending on the content of the second cell of a row. * A Row is an array of strings. * The table is an array of rows. * I want to sort table. * So I need to compare Rows. * So I thought I had to implement a compare() function that takes Rows as arguments and compares the specific string cell I desire. Here is the code I tried: ---------------------------- $ include "seed7_05.s7i"; include "stdio.s7i"; const type: Row is array string; const func integer: compare(in Row: A, in Row: B) is return compare(A[2], B[2]); const proc: main is func local var array Row : table is [] ( [] ("CCC", "BBB"), [] ("CCC", "AAA"), [] ("AAA", "CCC")); var Row : r is 2 times ""; begin for r range table do writeln(r[1] & " " & r[2]); end for; table:=sort(table); writeln("-----------"); for r range table do writeln(r[1] & " " & r[2]); end for; end func; ---------------------------- And it fails to compile, complaining that I redeclare the compare() function: ---------------------------- ~/test/seed7 $s7c compare_array.s7 SEED7 COMPILER Version 3.0 Copyright (c) 1990-2018 Thomas Mertes Source: compare_array.s7 Compiling the program ... *** compare_array.s7(8):34: (ref TEST_3 param, ref TEST_3 param) compare declared twice return compare(A[2], B[2]); -----------------------------------^ 1 error found ---------------------------- I imagine it has something to do with my Row type being not a true separate type, but rather some kind of alias to array? (I am knew to the language so I don't know if that even makes sense ;-) ) Anyway, what would be the right way(s) to achieve my goal? Faithfully yours, Stéphane Goujet. |