Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Sorting a column in Numbers

I'm trying to do something simple - I thought. I have a table and I want to pick one column and sort the items in THAT column only, and leave the rest of the table intact. I end up with the column sorted as I wished, but the ROWS are also being sorted (it appears in some logical order - but a logic I can't figure out). I think I've used every combinations available in the reorganize window, and I have tried multiple ways of selecting, etc., with no luck.


I've been using Numbers since it came out. I've done this simple operation many times before, but I've never had this prob before. Numbers 2.1 (436)

iMac, Mac OS X (10.5.2), Intel Core 2 Duo processor

Posted on Sep 4, 2011 6:18 PM

Reply
Question marked as Best reply

Posted on Sep 4, 2011 6:30 PM

Addendum: I've looked more thoroughly at related questions and the answer appears to be that you have to take the columns out to make a seperate table, do the operation there, and then replace them in the table???!!! AND a lot of unpleasant stabbing inbetween those simple lines. Frankly I am shocked the way everyone talks to each other in this discussion!


Is this still the answer - remove columns as above? If so it IS pretty poor (although I won't follow the examples of language provided above....).


Since I've done this many times before (not recently tho) and never had problems, what do you think is the difference?


Any suggestions other than removing the columns and then replacing them?

25 replies

Mar 17, 2013 12:56 AM in response to Dutch46

Dutch46 writes:

"The only math is in column D (Total) and it is as follows, D1 is a fixed value of $408.00. In the next row down the math in column D would be D1+C2, in row 3 it would be D2+C3 and so on. If I now add an entry on 2/25/13 and wish to sort the data in date order all I need to do is sort columns A-C in date order and the math built into column D takes care of the addition."


This sounds similar the the calculation done in the current balance column of the Checking Register template supplied with Numbers '09. Here's a sample with the columns you describe. I've assigned random dates to the transactions to provide three columns with different sort results. All sorts are 'ascending'.

Original order:

User uploaded file

Resorted on Date

User uploaded file

Resorted on Item:

User uploaded file

Formula, entered in D2, then filled down the rest of column D:

=OFFSET(D$1,ROW()-2,0)+OFFSET($C$1,ROW()-1,0)


Row 1 is defined as a Header row. Header rows are not included in sorts.


"Despite selecting the "sort selected columns" option, the application sorts all four columns carrying the values that are in columns D along with the sort."


Unless someone has slipped it in while I wasn't updating, there is no "sort selected columns" option in numbers '09. A sort may be based on values in one or more columns, and the user may choose to sort either the whole table or "selected rows," but it is always full rows that are sorted.


"The spreadsheet didn't survive the second attempt to send it either."


Spreadsheet (and other files) can't be attached to messages in these forums. You can post an image file, such as the screen shots in this post, using the camera icon at the top of the composition box.


To take a screen shot of part of the screen, place the mouse pointer at one corner of the area you want to include, then press shift-command-4. The arrow pointer will change to a crosshair.

Press and hold the (left) mouse button, and drag to create a rectangle enclosing the screen area wanted in the shot.

Release the mouse button to complete the shot.

The screen area will be captured as a .png file named Picture or Screenshot with either a number or the date and time added (depending on OS version).


To insert the image in your post:

Click the camera icon.

In the dialogue that opens, click Choose image.

Navigate to your Desktop, locate and click on the file to select it.

Click Choose.

Click Insert image.


Regards,

Barry

Mar 17, 2013 1:40 AM in response to Dutch46

Dutch46,


I note your reply (16 March 2013) to Barry's post on 4 September 2011. You highjacked a thread that is 18 months old and then you criticised Barry's reply.


These Apple Support Communities are user-to-user discussions. Volunteers give their time and knowledge to try to help.


Barry is a gentleman. I have followed Barry's contributions to various Apple Support Communities. Barry has always been helpful and constructive, contributing to the knowledge of the Original Poster whilst giving an insightful suggestion or solution. In my opinion, he has never 'castigated' anyone.


Barry was explaining that Numbers is not Excel, and Excel is not Numbers. Those two apps apply to two different ways of thinking. Excel is a spreadsheet and Numbers mimics a database.


Please read:


- https://discussions.apple.com/static/apple/tutorial/etiquette.html


- https://discussions.apple.com/static/apple/tutorial/tou.html


- the Numbers User Guide. You can download it from the Help Menu in Numbers.


- How To Ask Questions The Smart Way

http://www.catb.org/esr/faqs/smart-questions.html

Until you learn how to phrase your question, you will get no help from me.

Mar 17, 2013 8:16 AM in response to Barry

I did say "selected columns" and that was a mistake, it does say "selected rows". But you did put your finger on the problem when you stated that "A sort may be based on values in one or more columns, and the user may choose to sort either the whole table or "selected rows," but it is always full rows that are sorted. That is a serious problem. In a spreadsheet one may not always wish to sort the entire row because it may destroy data that one wishes to preserve in its original place. In large multi thousand cell spreadsheets one may only be dealing with a section of the data and forcing an unwanted sort will present problems and may well invalidate the results. As to the formula you gave me, why do I need to enter this complex piece of logic when a simple add this to that will do the trick? I wish to point out that a spreadsheet is supposed to be an aid to the user and kept as simple as possible.


I am 66 years old and have worked with spreadsheets in a professional capacity since they were invented over 40 years ago beginning with the very earliest ones that only added, subtracted, multiplied and divided numbers whose cells could not be moved from their original position in the spreadsheet. Their values could be changed, but the cell location couldn't be moved without redoing the entire spreadsheet. I have used many incarnations of spreadsheets from companies that no longer exist today. Some were good, some were not so good but the very best is Microsoft Exel with which I have worked extensively. It is the gold standard of spreadsheets and is remarkably versatile and the addition of Visual Basic has made it even more so. My only complaint with it was that they had a tendency to bury items in the menus and that, on very infrequent occasions, the 64K X64K limitation forced me to use other, less convenient methods of data analysis. But, that was what I was paid to do. The sort that I described to you works fine in my 2012 version of Excel so I will be using that instead of wrestling with the quirks of Numbers.


I bought the Numbers spreadsheet because it was cheap and I wanted to experiment with it to compare it to Excel. I will continue to play with it to see how it works but being unable to sort correctly puts a very severe limitation on experimentaion. I like the Apple operating system and use it whenever possible. It is much better from many viewpoints than the Microslush system. But, Microsoft does have some really good apps.


Once again I would like to thank you for your time, effort and promptness in replying to my questions. It is a far superior experience than any I have had with Microsoft products.


My Best Regards

Tom Belstler

tbelstler@gmail.com

Dutch46

Mar 17, 2013 9:00 AM in response to Dutch46

Tom,


Numbers is cheap, and it doesn't have all the bells and whistles of Excel, but it doesn't sort incorrectly either.


It follows the database model of always keeping the fields of a given record together. It will never take a field's entry from one record and attach it to another record, which is what would happen if you allowed the entries in one column to stay put and the entries in another column to sort.


Many of us here are as old and as experienced as you. Many much older. Our eyesight may fade and at some point we may be more easily confused, but I believe that those of us who posted to this thread can tell a spreadsheet gimmick from a protocol that conforms to standards. As Barry noted way up the thread, in Numbers you aren't stuck with a sea of cells that you need to partition when sorting. In Numbers one isolates related data into single-purpose tables, eliminating the need to exclude columns when sorting. If you like the monolithic look and feel of Excel, use it rather than trying to pound Numbers into the Excel mold.


Jerry

Mar 17, 2013 10:26 AM in response to Jerrold Green1

Tom,


In addition to what Barry and Jerry, have already posted... I have been in Electrical Engineering field for 20 years. I have been programming computers since my Commodore Vic 20 days (even had a 16 kB upgrade card). As anyone will tell you, there is a right tool for the job (sometimes more than one). There are tools that are definitely NOT the right tool.


For what you pay Numbers is a GREAT tool. It is not MS Excel and makes not attempt to be. You should know that Numbers cannot handle the number of rows and columns as excel. There are OTHER strenghts that it does have such as actually presenting data such as:

User uploaded file


In excel this would be a mess. I hope you can appreciate that people in this forum volunteer their time to answer questions. None of us work for Apple but we do have extensive knowledge of the ability to solve problems. We, however, have no more ability to affect change with Numbers than we have on the weather.


Once you see the strengths of Numbers you may see Excel in a different light. Keep opsting questions here to learn the Numbers way. Once you understand how segregating and organizing data makes for a simpler solution you may find Numbers more compelling.


Regards,

Wayne

Mar 19, 2013 12:42 PM in response to Dutch46

Hello


I understand your point very well and feel the same that spreadsheet should be capable of performing partial sort.

The script below is my attempt to fix it. Bad news is it is a bit noisy in making temporary table, moving range to it, sorting the range, moving the range back and deleting the temporary table. Good news is it works as expected.


Here're two recipes; one for script menu and another for services menu (preferable under 10.6 or later)


# Recipe 1 (via script menu)

1) Open /Applications/Utilities/AppleScript Editor.app; and

2) if script menu is not yet enabled, open Preferences… > General and enable "Show Script menu in menu bar"; and

3) copy the code listed below to new document; and

4) save it as compiled script or script bundle with name, e.g., "sort selection.scpt" or "sort selection.scptd", in ~/Library/Scripts/Applications/Numbers directory so that it appear in script menu in Numbers.


Usage (via script menu):

Select target range in Numbers, invoke the script via script menu, enter the sort specifications when asked and it will sort the selection range according to the sort specs.



# Recipe 2 (via services menu)

1) Open /Applications/Automator.app; and

2) choose "Service" template; and

3) drag "Run AppleScript" action from the left pane to the right pane; and

4) replace the existing template code with the code listed below; and

5) set the service attributes so that service receives [no input] in [Numbers.app], where [...] is set via drop down menu in Automator workflow editor window; and

6) save the workflow with name, e.g., "sort selection"; and

7) quit Automator.app.


To set keyboard shortcut for "sort selection" service:

8) open /Applications/System Preferences; and

9) select Keyboard > Keyboard Shortcuts; and

10) select Services in left pane; and

11) set keyboard shortcut for "sort selection" service as, e.g., command + shift + 6.


Usage (via services menu):

Select target range in Numbers, invoke the script via Services menu or keyboard shortcut if defined, enter the sort specifications when asked and it will sort the selection range according to the sort specs.



# Notes

* Sort specifications (specs) are in the form as

specs = spec[,spec,...]

spec = <column relative index in selection>[a|d]

a = ascending (default)

d = descending

e.g. = 1a,3d

which means to sort by 1st column in selection in ascending order and by 3rd column in selection in descending order.

The specs are in primary-key-first order, i.e., 1a is the primary key and 3d is the secondary key.

You can use 1,3d instead of 1a,3d because "a" is the default direction.


* Tested as script under 10.5.8 and also as Automator service with keyboard shortcut under 10.6.5.



# Script


sort selection.applescript

-------------------------

(*
    sort selection range only
    v0.2
*)
_main()
on _main()
    -- (0) get original selection specifiers
    tell application "Numbers"
        activate
        set {range:_range, table:_table, sheet:_sheet} to my _selection(document 1)
        tell _range
            if it is missing value then return
            set {ck, rk} to {count columns, count rows}
        end tell
    end tell
    
    -- (1) accept sort specifications
    tell application "Numbers"
        repeat
            display dialog ¬
                "Enter sort specifications (primary key first)" & return & ¬
                "  spec[,spec, ...]" & return & ¬
                "  spec = <column relative index in selection>[a|d]" & return & ¬
                "  a = ascending (default)," & return & ¬
                "  d = descending" & return & ¬
                "  e.g. = 1a,3d" default answer "1a"
            try
                set specs to my _split(",", text returned of result)
                repeat with s in specs
                    try
                        set col to s as integer
                        set dir to -1
                    on error
                        try
                            set col to s's text 1 thru -2 as integer
                            set dir to s's character -1
                            if dir = "a" then
                                set dir to -1
                            else if dir = "d" then
                                set dir to 1
                            else
                                error number 8000
                            end if
                        on error number errn
                            display dialog "invalid spec: " & s
                            error number errn
                        end try
                    end try
                    if col > ck or col < 1 then
                        display dialog "column index is out of range [1, " & ck & "]: " & col
                        error number 8001
                    end if
                    set s's contents to {col, dir}
                end repeat
                exit repeat
            on error number errn
                if errn = -128 then error number -128 -- user cancel
            end try
        end repeat
    end tell
    
    -- (2) mark to move the original selection range
    tell application "System Events"
        tell process "Numbers"
            keystroke "x" using {command down, shift down} -- edit > mark to move
        end tell
    end tell
    
    -- (3) make new work table and select range B2
    tell application "Numbers"
        tell _sheet
            set _wktable to make new table at end with properties {column count:ck + 1, row count:rk + 1}
            tell _wktable
                set selection range to range "B2"
            end tell
        end tell
    end tell
    
    -- (4) move the original selection range to work table
    tell application "System Events"
        tell process "Numbers"
            keystroke "v" using {command down, shift down} -- edit > move
        end tell
    end tell
    
    -- (5) sort the moved selection range in work table
    tell application "Numbers"
        set {range:_wkrange, table:_wktable} to my _selection(document 1)
        repeat with s in specs's reverse
            set {col, dir} to s's contents
            tell _wktable
                if dir = -1 then
                    sort by _wkrange's column col direction ascending in rows _wkrange
                else
                    sort by _wkrange's column col direction descending in rows _wkrange
                end if
            end tell
        end repeat
    end tell
    
    -- (6) mark to move the sorted selection range in work table
    tell application "System Events"
        tell process "Numbers"
            keystroke "x" using {command down, shift down} -- edit > mark to move
        end tell
    end tell
    
    -- (7) select the original selection range
    tell application "Numbers"
        tell _table
            set selection range to _range
        end tell
    end tell
    
    -- (8) move the sorted selection range in work table to orignal table
    tell application "System Events"
        tell process "Numbers"
            keystroke "v" using {command down, shift down} -- edit > move
        end tell
    end tell
    
    -- (9) delete the work table
    tell application "Numbers"
        delete _wktable
    end tell
end _main

on _selection(doc)
    (*
        reference doc : target document
        return record : {range:_range, table:_table, sheet:_sheet}
            _range = reference to named range in selection
            _table = table object to which selection range belongs
            _sheet = sheet object to which selection range belongs
    *)
    (*
        Limitation
            Numbers allows to select uncontinuous regions
            but its scripting interface does not provide decent method to retrieve them.
        
            If uncontinuous regions are selected, 'selection range' returns the minimum continuous region
            which includes all the regions in selection.
    *)
    script o
        property parent : {}
        property pp : {}
        local q, r, s, _range, _table, _sheet
        tell application "Numbers"
            set pp to doc's every sheet's every table's selection range as list
            repeat with p in my pp -- per sheet
                set q to p's every reference -- retrieve object (filtering out missing value)
                if q ≠ {} then
                    set q to q's item 1 -- selection range object [1]
                    set r to q as record -- selection range object specifier record [2]
                    set _table to r's every reference's item 1 -- container table reference [3]
                    set s to (a reference to _table's selection range) -- selection range reference [4]
                    set _range to (a reference to _table's range (s's name)) -- named range reference [5]
                    set _sheet to (_table as record)'s every reference's item 1 -- container sheet reference [3]
                    return {range:_range, table:_table, sheet:_sheet}
                end if
            end repeat
            return {range:missing value, table:missing value, sheet:missing value}
        end tell
        (*
            [1] class specifier for 'range' is broken in Numbers 09
            [2] «class want» value is broken in Numbers 09
            [3] simple method to get «class from» value without asking for «class from» key which causes trouble in recompilation of the token 'from'.
            [4] proper reference of selection range object
            [5] proper reference of named range object
        *)
    end script
    tell o to run
end _selection

on _split(d, t)
    local astid, astid0, tt
    set astid to a reference to AppleScript's text item delimiters
    try
        set {astid0, astid's contents} to {astid's contents, {d}}
        set tt to t's text items
        set astid's contents to astid0
    on error errs number errn
        set astid's contents to astid0
        error errs number errn
    end try
    return tt
end _split

-------------------------


Hope this may help,

H

Sorting a column in Numbers

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.