How do I set a default sort in ag-grid?
- User
How do I set a default sort in ag-grid?
Assistant
In ag-Grid, you can set a default sort in several ways:
- Using Column Definitions:
columnDefs = [ { field: 'columnName', sort: 'asc' // or 'desc' } ]- Using Grid Options:
gridOptions = { defaultColDef: { sortable: true }, sortingOrder: ['desc', 'asc', null], // Optional: customize sort order defaultSortModel: [ { colId: 'columnName', sort: 'asc' } ] }- Programmatically:
// After grid is initialized gridApi.setSortModel([ { colId: 'columnName', sort: 'asc' } ])- Multiple Column Sort:
gridOptions = { defaultSortModel: [ { colId: 'column1', sort: 'asc' }, { colId: 'column2', sort: 'desc' } ] }Example with React:
const GridExample = () => { const gridOptions = { columnDefs: [ { field: 'name', sortable: true, sort: 'asc' // Default sort for this column } ], defaultSortModel: [ { colId: 'name', sort: 'asc' } ] }; return <AgGridReact gridOptions={gridOptions} />; }Choose the method that best fits your specific use case and implementation.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.