Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 35023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:10:13+00:00 2026-05-10T14:10:13+00:00

I have a datagrid, populated as shown below. When the user clicks on a

  • 0

I have a datagrid, populated as shown below. When the user clicks on a column header, I would like to sort the rows using a lexicographic sort in which the selected column is used first, then the remaining columns are used in left-to-right order to break any ties. How can I code this?

(I have one answer, which I’ll post below, but it has a problem — I’ll be thrilled if somebody can provide a better one!)

Here’s the layout:

<?xml version='1.0' encoding='utf-8'?>  <mx:Application xmlns:mx='http://www.adobe.com/2006/mxml'      layout='absolute' creationComplete='onCreationComplete()'>      <mx:Script source='GridCode.as' />      <mx:DataGrid id='theGrid' x='61' y='55' width='466' height='317'>         <mx:columns>             <mx:DataGridColumn dataField='A'/>             <mx:DataGridColumn dataField='B'/>             <mx:DataGridColumn dataField='C'/>         </mx:columns>     </mx:DataGrid>  </mx:Application> 

And here’s the backing code:

import mx.collections.ArrayCollection; import mx.collections.Sort; import mx.collections.SortField; import mx.controls.dataGridClasses.DataGridColumn; import mx.events.DataGridEvent;  public function onCreationComplete():void {     var ar:ArrayCollection = new ArrayCollection();     var ob:Object;     for( var i:int=0; i<20; i++ )     {         ob = new Object();         ob['A'] = i;         ob['B'] = i%3;         ob['C'] = i%5;         ar.addItem(ob);     }     this.theGrid.dataProvider = ar; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:10:13+00:00Added an answer on May 10, 2026 at 2:10 pm

    The best answer I’ve found so far is to capture the headerRelease event when the user clicks:

    <mx:DataGrid id='theGrid' x='61' y='55' width='466' height='317'         headerRelease='onHeaderRelease(event)'> 

    The event handler can then apply a sort order to the data:

    private var lastIndex:int = -1; private var desc:Boolean = false;  public function onHeaderRelease(evt:DataGridEvent):void {     evt.preventDefault();      var srt:Sort = new Sort();     var fields:Array = new Array();      if( evt.columnIndex == lastIndex )     {         desc = !desc;     }     else     {         desc = false;         lastIndex = evt.columnIndex;     }      fields.push( new SortField( evt.dataField, false, desc ) );     if( evt.dataField != 'A' )         fields.push( new SortField('A', false, desc) );     if( evt.dataField != 'B' )         fields.push( new SortField('B', false, desc) );     if( evt.dataField != 'C' )         fields.push( new SortField('C', false, desc) );     srt.fields = fields;      var ar:ArrayCollection = this.theGrid.dataProvider as ArrayCollection;     ar.sort = srt;     ar.refresh(); } 

    However this approach has a well-known problem, which is that the column headers no longer display little arrows to show the sort direction. This is a side-effect of calling evt.preventDefault() however you must make that call or else your custom sort won’t be applied.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 244k
  • Answers 244k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is a wrapped exception. What's the root cause of… May 13, 2026 at 8:08 am
  • Editorial Team
    Editorial Team added an answer I would recommend using JSON. On the server, you would… May 13, 2026 at 8:08 am
  • Editorial Team
    Editorial Team added an answer public Person[] people = new Person[100]; public is an access… May 13, 2026 at 8:08 am

Related Questions

My question: How do I bind the SelectedItem from a primary datagrid to the
I have a datagrid which is populated with CSV data when the user drag/drops
I have a DataGrid that is populated by an xml response of a web
Hopefully someone can shed a little light on an issue that I'm currently having

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.