+ Reply to Thread
Results 1 to 4 of 4

Sorting Array

  1. #1
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    593

    Sorting Array

    Hi,
    I have an Array which has 3 columns:
    Data1 2 23
    Data2 5 11
    Data3 1 15
    Data4 1 17


    I have declared the Array as MyArray ( 0 to 500, 0 to 2)

    I want to sort the Array by the 3rd column in descending order.
    Ho do I do this?
    Thanks

  2. #2
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Sorting Array

    One tricky way to achieve this is, insert a new sheet and write your array onto it and sort the third column and rewrite the sorted values back into array and delete the sheet you inserted like below...
    Please Login or Register  to view this content.
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Sorting Array

    One way to do this would be to create a one dimensional indexing array and sort that array on the custom LT function.

    Please Login or Register  to view this content.
    The indexing array is an array of row numbers and LT returns whether the third column of the indicated row is less than the other.

    The full routine would look like this.
    Please Login or Register  to view this content.
    This uses a bubble sort algorithm and for small data sets sktneer's use of Excel's built in sort is faster, but for larger data sets the indexing array approach is faster if one uses a quick sort rather than a bubble sort.

    I've generalized this routine quite a bit. The array does not need to be 0-based and the SortDescending variable could be used to sort ascending.
    Last edited by mikerickson; 11-06-2016 at 02:09 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    273

    Re: Sorting Array

    Below is some code for a quicker sort than the bubble sort if you can put your data into a userdefined type such as struct_State below. Basic sort code is copied from a posting on the internet and adapted for userdefined types..I'm not good enough to write the code from scratch.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Sorting an Array
    By vba_beginner in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-24-2013, 08:38 AM
  2. Sorting an Array
    By vba_beginner in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-23-2013, 11:19 PM
  3. Sorting an array
    By lukela85 in forum Excel General
    Replies: 0
    Last Post: 02-12-2013, 04:17 PM
  4. Excel 2007 : Sorting by array
    By ncurran217 in forum Excel General
    Replies: 12
    Last Post: 12-02-2011, 12:52 PM
  5. Sorting an array
    By lukela85 in forum Excel General
    Replies: 10
    Last Post: 07-09-2010, 08:10 PM
  6. Array sorting
    By JXBlack in forum Excel General
    Replies: 2
    Last Post: 01-29-2010, 08:44 PM
  7. sorting an array
    By johnboy12 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-12-2008, 11:36 PM
  8. [SOLVED] Sorting an Array
    By Mike Archer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-28-2006, 02:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1