sorting dont work on string column that conatins numeric + string data

sorting dont work on string column that conatins numeric + string data

293 Days Ago
293 Days Ago
289 Days Ago
289 Days Ago
288 Days Ago

3 Replies

TS Thiyagu Subramani Syncfusion Team April 24, 2020 08:21 AM UTC

Hi Abdul, 

Thanks for contacting Syncfusion support. 

Query : String column that conatins numeric + string data 
 
By default in EJ2 Grid we can able to perform sort operation only on individual type(Numeric, string, date, boolean etc.,) columns and it is not feasible to perform sort operation contains numeric with string data. 
 
To achieve your requirement we suggest you to use the custom sort and it can achieved by defining the sortComparer property. The sort comparer function has the same functionality like array sort comparer. Find below code snippets for your reference. 

@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.datasource).Columns(col => 
        { 
 
        col.Field("OrderID").HeaderText("Order ID").Width("120").IsPrimaryKey(true).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
        col.Field("CustomerID").HeaderText("Order Date").Width("130").SortComparer("sortComparer").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
        }).Height("400").AllowPaging().Toolbar(new List<string> 
            () { "Add", "Edit", "Delete", "Update", "Cancel" }).AllowSorting(true).AllowFiltering().FilterSettings(filter => filter.Type(Syncfusion.EJ2.Grids.FilterType.Menu)).EditSettings(edit => { edit.AllowEditing(true).AllowAdding(true).AllowDeleting(true); }).Render() 
</div> 
 
<script type="text/javascript"> 
    function sortComparer(reference, comparer, refrenceObj, comparerObj) { 
        var refC = parseInt(reference.replace(" Days Ago", "")); 
        var comC = parseInt(comparer.replace(" Days Ago", "")) 
        if (refC < comC) { 
            return -1; 
        } 
        if (refC > comC) { 
            return 1; 
        } 
        return 0; 
    } 
</script> 


public void BindData() 
        { 
            int code = 10000; 
            for (int i = 1; i < 10; i++) 
            { 
                orddata.Add(new OrdersDetails(code + 2, "293 Days Ago", i + 0, 3.3 * i, true, new DateTime(1995, 7, 2, 2, 3, 5), "Madrid", "Queen Cozinha", "Brazil", new DateTime(1996, 9, 11), "Avda. Azteca 123")); 
                orddata.Add(new OrdersDetails(code + 3, "292 Days Ago", i + 1, 4.3 * i, true, new DateTime(2012, 12, 25, 2, 3, 5), "Cholchester", "Frankenversand", "Germany", new DateTime(1996, 10, 7), "Carrera 52 con Ave. Bolívar #65-98 Llano Largo")); 
                orddata.Add(new OrdersDetails(code + 4, "288 Days Ago", i + 2, 5.3 * i, false, new DateTime(2002, 12, 25, 2, 3, 5), "Marseille", "Ernst Handel", "Austria", new DateTime(1996, 12, 30), "Magazinweg 7")); 
                orddata.Add(new OrdersDetails(code + 5, "1289 Days Ago", i + 3, 6.3 * i, true, new DateTime(1953, 02, 18, 05, 2, 4), "Tsawassen", "Hanari Carnes", "Switzerland", new DateTime(1997, 12, 3), "1029 - 12th Ave. S.")); 
                code += 5; 
                } 
        } 




Please get back to us, if you need any further assistance. 

Regards, 
Thiyagu S. 



AW Abdul Wahab May 8, 2020 09:08 PM UTC

How can i check in my custom sort function that sort direction is descending or ascending



TS Thiyagu Subramani Syncfusion Team May 11, 2020 07:28 AM UTC

Hi Abdul, 

Thanks for the update. 

Query : How can i check in my custom sort function that sort direction is descending or ascending 
 
By default, the sorting order will be as ascending -> descending -> none.  


To get the sort direction we suggest you to use actionBegin event of EJ2 Grid. The actionBegin event will be triggered before our custom sort function called. In this event we can get the sort direction in the arguments 

Please refer to the code below. 

var direction; 
    function actionBegin(args) { 
        if (args.requestType === 'sorting') { 
            direction = args.direction;  // you can get sort direction using this code.. 
        } 
    } 
    function sortComparer(reference, comparer, refrenceObj, comparerObj) { 
        console.log(direction); 
        . . . . . . .  
    } 


If allowUnsort set to false the user cannot get the grid in unsorted state by clicking the sorted column header. 



please get back to us, if you need any further assistance. 

Regards, 
Thiyagu S 


Loader.
Up arrow icon