Forum Discussion

ddpl's avatar
ddpl
Icon for Solution Sage rankSolution Sage
3 years ago
Solved

How List.Sort work???

I want to understand how below M Code work, more explicitly.   = List.Sort( { "Feb", "Jan", "Feb", "Jan", "Mar" }, ( x, y ) => Value.Compare( ...
  • ddpl's avatar
    ddpl
    3 years ago

    Anonymous ,

     

    Your explaination did explaine alot about the behaviour of List.Sort, Thanks for that.

     

    I want to ask further that below M-Codes give me exact same result...

     

    = List.Sort( { "Feb", "Jan", "Feb", "Jan", "Mar" }, 
    ( x, y ) => Value.Compare(
    List.PositionOf( { "Jan","Feb","Mar" }, x ),
    List.PositionOf( { "Jan","Feb","Mar" }, y ) ) )
    = List.Sort( { "Feb", "Jan", "Feb", "Jan", "Mar" }, 
    ( x, y ) => Value.Compare(
    List.PositionOf( { "Jan","Feb","Mar" }, x ),
    List.PositionOf( { "Mar","Feb","Jan" }, y ) ) )
    = List.Sort( { "Feb", "Jan", "Feb", "Jan", "Mar" }, 
    ( x, y ) => Value.Compare(
    List.PositionOf( { "Jan","Feb","Mar" }, x ),
    List.PositionOf( { "Mar","Jan","Feb" }, y ) ) )

    Result is... { "Jan", "Jan", "Feb", "Feb", "Mar" } for all 3 of them.

     

    I want to know that if position/order of list items inside of List.PositionOf statment did not match, how List.Sort behave...?

     

    Hope it makes sense.