dynamics-crmsql-order-bycrmquery-expressions

Order by a string by numbers with QueryExpression


I'm having a problem ordering numbers that are saved as string in CRM,

Its working fine until 10, then it says that 9 > 10 I know a simple solution where I can append zeros to the strings into a fixed length. Wondering if there is a way to order by a string by int in some way.

My code:

       QueryExpression query = new QueryExpression(entity);
       query.ColumnSet.AddColumn(ID);
       query.AddOrder(ID, OrderType.Descending); //there is a problem because the type is string.
       EntityCollection entityCollection = organizationService.RetrieveMultiple(query);

Solution

  • I don't think there is any easy way of achieving this. I faced the same issue for the post code and ended up storing both values i.e. string and int. And while querying i used the int field to sort it.