in reply to SQLite (DBD) Sorting

You could sort using a routine to decompose the accented letter to their base characters. Something like this perhaps.

sub deaccent { my $phrase = shift; return $phrase unless ( $phrase =~ y/\xC0-\xFF// ); #short circuit + if no upper chars # translterate what we can (for speed) $phrase =~ tr/ÀÁÂÃÄÅàáâãäåÇçÈÉÊËèéêëÌÍÎÏìíîïÒÓÔÕÖØòóôõöøÑñÙÚÛÜùúûü +Ýÿý/AAAAAAaaaaaaCcEEEEeeeeIIIIiiiiOOOOOOooooooNnUUUUuuuuYyy/; # and substitute the rest my %trans = qw(Æ AE æ ae Þ TH þ th Ð TH ð th ß ss); $phrase =~ s/([ÆæÞþÐðß])/$trans{$1}/g; return $phrase; }

used as:

foreach my $row_db ( sort { deaccent($a) cmp deaccent($b) } @$selected + ) {
foreach my $row_db ( sort { deaccent($a->[1]) cmp deaccent($b->[1]) } +@$selected ) { my ($ID, $column1, $column2) = @$row_db; print "$column1\n"; }

Or as a Schwartzian Transform. (probably not worth it unless @$selected is large)

foreach my $row_db (map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { + [$_, deaccent($_)] } @$selected ) {
foreach my $row_db (map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { + [$_, deaccent($_->[1])] } @$selected ) { my ($ID, $column1, $column2) = @$row_db; print "$column1\n"; }

Note: this only covers ISO-Latin-1, if you need other code pages, modify deaccent() to suit (or possibly use Text::Unidecode)

UPDATE: fixed errors.

Replies are listed 'Best First'.
Re^2: SQLite (DBD) Sorting
by Anonymous Monk on Apr 29, 2010 at 18:19 UTC

    Thank you for your reply. What I don't get is how you are going to limit the sorting to only one specific database column (in the example column1) when using your first solution:

    foreach my $row_db ( sort { deaccent($a) cmp deaccent($b) } @$selected + ) { my ($ID, $column1, $column2) = @$row_db; print "$column1\n"; }

    I implemented your approach, but I get a "no ordered" output, probably due to the missing reference to the column I want to sort.

    I'd really hope to get your answer!

      sort { deaccent($a) cmp deaccent($b) } @$selected
      should be
      sort { deaccent($a->[1]) cmp deaccent($b->[1]) } @$selected
      and
      map $_->[0], sort $a->[1] cmp $b->[1], map [ $_, deaccent($_) ], @$selected
      should be
      map $_->[0], sort $a->[1] cmp $b->[1], map [ $_, deaccent($_->[1]) ], @$selected

      Ah. My bad. You need to access the correct column in the row array.

      foreach my $row_db ( sort { deaccent($a->[1]) cmp deaccent($b->[1]) } +@$selected ) { my ($ID, $column1, $column2) = @$row_db; print "$column1\n"; }

        It works perfectly! Thank you very much! Cla