Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-5-haiku-20241022· AnthropicHugging Face LMArena Human Preference 140K

How do I set a default sort in ag-grid?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o3-mini’s answer.
  1. User

    How do I set a default sort in ag-grid?

  2. Claude Assistant

    In ag-Grid, you can set a default sort in several ways:

    1. Using Column Definitions:
    columnDefs = [
      {
        field: 'columnName',
        sort: 'asc' // or 'desc'
      }
    ]
    
    1. Using Grid Options:
    gridOptions = {
      defaultColDef: {
        sortable: true
      },
      sortingOrder: ['desc', 'asc', null], // Optional: customize sort order
      defaultSortModel: [
        { colId: 'columnName', sort: 'asc' }
      ]
    }
    
    1. Programmatically:
    // After grid is initialized
    gridApi.setSortModel([
      { colId: 'columnName', sort: 'asc' }
    ])
    
    1. 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.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.

Identical first prompt