Not your normal sort!

Posts   
 
    
Posts: 40
Joined: 26-Nov-2004
# Posted on: 15-Mar-2005 14:15:22   

We had a question from one of our customers today regarding the sorting of their search results. I will use a fruit for an example.

Apple A Plum Banana Orange Pear The Kiwi

Now really they should be sorted as

Apple Banana The Kiwi Orange Pear A Plum

So the 'A' and 'The' are ignored, does anyone know how to do this using the mapper?

Cheers

Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 15-Mar-2005 14:20:02   

This is going to be complex and certainly not something I have seen the mapper do out of the box. You will need to fetch the entities and sort in memory using the a custom IComparer which ignores the "A ", "The " etc in the compare evaluations.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 15-Mar-2005 14:35:37   

I too wouldn't know how to do this, even in sql (perhaps a string split, but even then I don't know if it works).

Frans Bouma | Lead developer LLBLGen Pro
Posts: 40
Joined: 26-Nov-2004
# Posted on: 15-Mar-2005 14:58:18   

Yes, I think I'm going to do it client side. Thanks, but it was worth a stab.

wvnoort
User
Posts: 96
Joined: 06-Jan-2005
# Posted on: 15-Mar-2005 15:14:59   

You didn't say what database you are using, but there is possible method in SQL Server.

Add a computed column to the table:


create table SortTest (
ID int identity,
Description varchar(50),
SortKey as 
  case
    when charindex('a ', Description) = 1 then substring(Description, 3, len(Description))
    when charindex('the ', Description) = 1 then substring(Description, 5, len(Description))
    else Description
  end
)

Now you can use an orderby on sortKey.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 15-Mar-2005 15:23:25   

Clever! simple_smile

Frans Bouma | Lead developer LLBLGen Pro
davisg avatar
davisg
User
Posts: 113
Joined: 27-Feb-2005
# Posted on: 15-Mar-2005 22:05:18   

Hi, We had the same problem in the music industry for Artist names, i.e. Kate Bush would normally list under Bush and not Kate. We solved this by having a sortkey column in the table and therefore sorted on that.

example: -

Artist Bon Jovi with SortKey of Bon Jovi Artist Kate Bush with SortKey of Bush Kate Artist Jon Bon Jovi with SortKey of Bon Jovi Jon

listing would be: -

Bon Jovi Jon Bon Jovi Kate Bush

Geoff.

JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 15-Mar-2005 22:45:33   

Guys, just add a SortOrder column to your table and always sort by that. Although it faintly rings of denormalization, it's a perfectly legitimate thing to do.

sunglasses