emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Sorting table columns (*not* content)
@ 2010-11-01 13:42 Gary
  2010-11-01 13:52 ` Jeff Horn
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Gary @ 2010-11-01 13:42 UTC (permalink / raw)
  To: emacs-orgmode

Is there any way to sort the columns of a table, such that for example

| Col 3 | Col 1 | Col 2 |

can be converted to

| Col 1 | Col 2 | Col 3 |
|-------+-------+-------|
| ...   | ...   | ...   |

?

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Sorting table columns (*not* content)
  2010-11-01 13:42 Sorting table columns (*not* content) Gary
@ 2010-11-01 13:52 ` Jeff Horn
  2010-11-01 14:15   ` Gary
  2010-11-01 19:02   ` Christian Moe
  2010-11-01 14:43 ` Juan Pechiar
  2010-11-01 17:47 ` Dan Davison
  2 siblings, 2 replies; 14+ messages in thread
From: Jeff Horn @ 2010-11-01 13:52 UTC (permalink / raw)
  To: emacs-orgmode

From the manual: [1]
,----
| C-c C-x M-w
| Copy a rectangular region from a table to a special clipboard. Point
and mark determine edge fields of the rectangle. If there is no active
region, copy just the current field. The process ignores horizontal
separator lines.
| C-c C-x C-w
| Copy a rectangular region from a table to a special clipboard, and
blank all fields in the rectangle. So this is the “cut” operation.
| C-c C-x C-y
| Paste a rectangular region into a table. The upper left corner ends
up in the current field. All involved fields will be overwritten. If
the rectangle does not fit into the present table, the table is
enlarged as needed. The process ignores horizontal separator lines.
`----

Hope this helps,
Jeff

* Footnotes
[1] http://orgmode.org/org.html#Built_002din-table-editor

-- 
Jeffrey Horn
Graduate Lecturer and PhD Student in Economics
George Mason University

(704) 271-4797
jhorn@gmu.edu
jrhorn424@gmail.com

http://www.failuretorefrain.com/jeff/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Sorting table columns (*not* content)
  2010-11-01 13:52 ` Jeff Horn
@ 2010-11-01 14:15   ` Gary
  2010-11-01 15:09     ` Jeff Horn
  2010-11-01 19:44     ` Carsten Dominik
  2010-11-01 19:02   ` Christian Moe
  1 sibling, 2 replies; 14+ messages in thread
From: Gary @ 2010-11-01 14:15 UTC (permalink / raw)
  To: emacs-orgmode

Jeff Horn wrote:

> Hope this helps,

Not much, really. You're saying that I can do rectangle editing, which I
knew. Are you basically saying "you have to sort them yourself"? (If so,
that's okay, it's a valid answer, even if it isn't the one I want!)

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Sorting table columns (*not* content)
  2010-11-01 13:42 Sorting table columns (*not* content) Gary
  2010-11-01 13:52 ` Jeff Horn
@ 2010-11-01 14:43 ` Juan Pechiar
  2010-11-01 15:33   ` Gary
  2010-11-01 15:39   ` brian powell
  2010-11-01 17:47 ` Dan Davison
  2 siblings, 2 replies; 14+ messages in thread
From: Juan Pechiar @ 2010-11-01 14:43 UTC (permalink / raw)
  To: emacs-orgmode

If you don't mind losing h-lines, you can transpose the table, sort
rows, and transpose back.

This code (which I previously posted to the list) transposes a table:

#+begin_src: emacs-lisp

(defun org-transpose-table-at-point ()
  "Transpose orgmode table at point, eliminate hlines"
  (interactive)
  (let ((contents (apply #'mapcar* #'list
                         (remove-if-not 'listp  ;; remove 'hline from list
                                        (org-table-to-lisp))))  ;; signals error if not table
        )
    (delete-region (org-table-begin) (org-table-end))
    (insert (mapconcat (lambda(x) (concat "| " (mapconcat 'identity x " | " ) "  |\n" ))
                       contents
                       ""))
    (org-table-align)
    )
)

#+end_src

Regards,
.j.

On Mon, Nov 01, 2010 at 02:42:55PM +0100, Gary wrote:
> Is there any way to sort the columns of a table, such that for example
>
> | Col 3 | Col 1 | Col 2 |
>
> can be converted to
>
> | Col 1 | Col 2 | Col 3 |
> |-------+-------+-------|
> | ...   | ...   | ...   |
>
> ?

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Re: Sorting table columns (*not* content)
  2010-11-01 14:15   ` Gary
@ 2010-11-01 15:09     ` Jeff Horn
  2010-11-01 19:44     ` Carsten Dominik
  1 sibling, 0 replies; 14+ messages in thread
From: Jeff Horn @ 2010-11-01 15:09 UTC (permalink / raw)
  To: emacs-orgmode

On Mon, Nov 1, 2010 at 10:15 AM, Gary <emacs-orgmode@garydjones.name> wrote:
> Jeff Horn wrote:
>
>> Hope this helps,
>
> Not much, really. You're saying that I can do rectangle editing, which I
> knew. Are you basically saying "you have to sort them yourself"? (If so,
> that's okay, it's a valid answer, even if it isn't the one I want!)

Cool. I don't know what other people know, so I thought I'd offer how
I've solved the problem in the past. Better to respond when I can than
see a question linger unanswered on the list.

Have a good one,
Jeff

-- 
Jeffrey Horn
Graduate Lecturer and PhD Student in Economics
George Mason University

(704) 271-4797
jhorn@gmu.edu
jrhorn424@gmail.com

http://www.failuretorefrain.com/jeff/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Sorting table columns (*not* content)
  2010-11-01 14:43 ` Juan Pechiar
@ 2010-11-01 15:33   ` Gary
  2010-11-01 15:39   ` brian powell
  1 sibling, 0 replies; 14+ messages in thread
From: Gary @ 2010-11-01 15:33 UTC (permalink / raw)
  To: emacs-orgmode

Juan Pechiar wrote:

> If you don't mind losing h-lines, you can transpose the table, sort
> rows, and transpose back.

Npghnyyl V jnag gb fbeg ba gur pbyhza urnqvatf, naq V nffhzr gung vf
jung lbh ner gnyxvat nobhg.

OGJ, vs lbh znxr vg qvssvphyg sbe zr gb ernq lbhe erfcbafr ol gbc
cbfgvat, qba'g rkcrpg zr gb qb nal qvssrerag.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Sorting table columns (*not* content)
  2010-11-01 14:43 ` Juan Pechiar
  2010-11-01 15:33   ` Gary
@ 2010-11-01 15:39   ` brian powell
  1 sibling, 0 replies; 14+ messages in thread
From: brian powell @ 2010-11-01 15:39 UTC (permalink / raw)
  To: Juan Pechiar; +Cc: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 2044 bytes --]

Jeff Horn seems to be explaining specifics of how to do some nitty-gritty
manipulations--he mentions (quoting the manual) methods for yanking, Ccxy

The methods Jeff expressed are useful for the question posed.

I recommend rectangle-kill and rectangle-yank--Cxrk and Cxry to solve this
(in a very simple albeit possibly faster or slower way--depending on how
many columns you want to move/sort---moving column/rectangle 3 to the 3rd
position in this case--took a few seconds)

I tested Juan's method/defun---works grrrrreat!!
Thanks Juan

P.S. I did lose the hlines like you reported; but, to put the line back was
3 key strokes: |-Tab

On Mon, Nov 1, 2010 at 10:43 AM, Juan Pechiar <juan@pechiar.com> wrote:

> If you don't mind losing h-lines, you can transpose the table, sort
> rows, and transpose back.
>
> This code (which I previously posted to the list) transposes a table:
>
> #+begin_src: emacs-lisp
>
> (defun org-transpose-table-at-point ()
>  "Transpose orgmode table at point, eliminate hlines"
>  (interactive)
>  (let ((contents (apply #'mapcar* #'list
>                         (remove-if-not 'listp  ;; remove 'hline from list
>                                        (org-table-to-lisp))))  ;; signals
> error if not table
>        )
>    (delete-region (org-table-begin) (org-table-end))
>    (insert (mapconcat (lambda(x) (concat "| " (mapconcat 'identity x " | "
> ) "  |\n" ))
>                       contents
>                       ""))
>    (org-table-align)
>    )
> )
>
> #+end_src
>
> Regards,
> .j.
>
> On Mon, Nov 01, 2010 at 02:42:55PM +0100, Gary wrote:
> > Is there any way to sort the columns of a table, such that for example
> >
> > | Col 3 | Col 1 | Col 2 |
> >
> > can be converted to
> >
> > | Col 1 | Col 2 | Col 3 |
> > |-------+-------+-------|
> > | ...   | ...   | ...   |
> >
> > ?
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>

[-- Attachment #1.2: Type: text/html, Size: 2847 bytes --]

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Sorting table columns (*not* content)
  2010-11-01 13:42 Sorting table columns (*not* content) Gary
  2010-11-01 13:52 ` Jeff Horn
  2010-11-01 14:43 ` Juan Pechiar
@ 2010-11-01 17:47 ` Dan Davison
  2010-11-01 18:44   ` brian powell
  2010-11-02 11:03   ` Gary
  2 siblings, 2 replies; 14+ messages in thread
From: Dan Davison @ 2010-11-01 17:47 UTC (permalink / raw)
  To: Gary; +Cc: emacs-orgmode

Gary <emacs-orgmode@garydjones.name> writes:

> Is there any way to sort the columns of a table, such that for example
>
> | Col 3 | Col 1 | Col 2 |
>
> can be converted to
>
> | Col 1 | Col 2 | Col 3 |
> |-------+-------+-------|
> | ...   | ...   | ...   |
>

I'd do this using an external language that has a matrix/table data type
with column indexing.

#+tblname: unsorted
| Col3 | Col1 | Col2 |
|------+------+------|
| c    | a    |    b |

#+source: sorted
#+begin_src R :var tab=unsorted :colnames yes
tab[,order(colnames(tab))]
#+end_src

#+results: sorted
| Col1 | Col2 | Col3 |
|------+------+------|
| a    | b    | c    |

Dan

> ?
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Re: Sorting table columns (*not* content)
  2010-11-01 17:47 ` Dan Davison
@ 2010-11-01 18:44   ` brian powell
  2010-11-02 11:03   ` Gary
  1 sibling, 0 replies; 14+ messages in thread
From: brian powell @ 2010-11-01 18:44 UTC (permalink / raw)
  To: Dan Davison; +Cc: Gary, emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 274 bytes --]

BTW this is what Gary rot13'd/cyphered:

Npghnyyl V jnag gb fbeg ba gur pbyhza urnqvatf, naq V nffhzr gung vf
jung lbh ner gnyxvat nobhg.

=>

"Actually I want to sort on the column headings, and I assume that is
what you are talking about."

Thanks for the questions Gary.

[-- Attachment #1.2: Type: text/html, Size: 433 bytes --]

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Sorting table columns (*not* content)
  2010-11-01 13:52 ` Jeff Horn
  2010-11-01 14:15   ` Gary
@ 2010-11-01 19:02   ` Christian Moe
  2010-11-01 19:27     ` brian powell
  1 sibling, 1 reply; 14+ messages in thread
From: Christian Moe @ 2010-11-01 19:02 UTC (permalink / raw)
  To: Jeff Horn; +Cc: emacs-orgmode

If you want to sort table columns manually (usually that's quick and 
easy), forget rectangle editing: M-left and M-right are your friends.

Cheers,
Christian


On 11/1/10 2:52 PM, Jeff Horn wrote:
>> From the manual: [1]
> ,----
> | C-c C-x M-w
> | Copy a rectangular region from a table to a special clipboard. Point
> and mark determine edge fields of the rectangle. If there is no active
> region, copy just the current field. The process ignores horizontal
> separator lines.
> | C-c C-x C-w
> | Copy a rectangular region from a table to a special clipboard, and
> blank all fields in the rectangle. So this is the “cut” operation.
> | C-c C-x C-y
> | Paste a rectangular region into a table. The upper left corner ends
> up in the current field. All involved fields will be overwritten. If
> the rectangle does not fit into the present table, the table is
> enlarged as needed. The process ignores horizontal separator lines.
> `----
>
> Hope this helps,
> Jeff
>
> * Footnotes
> [1] http://orgmode.org/org.html#Built_002din-table-editor
>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Sorting table columns (*not* content)
  2010-11-01 19:02   ` Christian Moe
@ 2010-11-01 19:27     ` brian powell
  0 siblings, 0 replies; 14+ messages in thread
From: brian powell @ 2010-11-01 19:27 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 615 bytes --]

Ah wow!  Thanks Christian!  I, at least, am very happy and content about
this subject-thread!

MLeft and MRight work great in TableMode of EmacsOrgMode!

Agree too: Rectangle editing is slower than Christian's method--EmacsOrgMode
has blown me away all over again!

Christian's method is a manual one though, Juan's and Dan's are program/app
based--there is more than one way to skin a cat.

All of the methods @do@ help one sort columns (in the way Gary described) in
OrgMode.

But still, playing devil's advocate here: There are those disappearing
hlines in Juan's method--maybe there is some code to write here.

[-- Attachment #1.2: Type: text/html, Size: 800 bytes --]

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Re: Sorting table columns (*not* content)
  2010-11-01 14:15   ` Gary
  2010-11-01 15:09     ` Jeff Horn
@ 2010-11-01 19:44     ` Carsten Dominik
  2010-11-02 11:05       ` Gary
  1 sibling, 1 reply; 14+ messages in thread
From: Carsten Dominik @ 2010-11-01 19:44 UTC (permalink / raw)
  To: Gary; +Cc: emacs-orgmode


On Nov 1, 2010, at 3:15 PM, Gary wrote:

> Jeff Horn wrote:
>
>> Hope this helps,
>
> Not much, really. You're saying that I can do rectangle editing,  
> which I
> knew. Are you basically saying "you have to sort them yourself"? (If  
> so,
> that's okay, it's a valid answer, even if it isn't the one I want!)

I guess in many cases it will be fast to sort by hand in a situation
like this.  Maybe not using rectangle commands, but using the commands
for dragging along a column, M-right and M-left.

If you have many columns, you can use a transposition as indicated
very cleverly by Juan.  Or you can write the command and send us a  
patch :D
Maybe just a bubble sort, using the column-shifting commands might be  
good enough.

How many columns do you have?

Cheers.

- Carsten

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Sorting table columns (*not* content)
  2010-11-01 17:47 ` Dan Davison
  2010-11-01 18:44   ` brian powell
@ 2010-11-02 11:03   ` Gary
  1 sibling, 0 replies; 14+ messages in thread
From: Gary @ 2010-11-02 11:03 UTC (permalink / raw)
  To: emacs-orgmode

Dan Davison wrote:
> Gary writes:
>
>> Is there any way to sort the columns of a table, such that for example
>>
>> | Col 3 | Col 1 | Col 2 |
>>
>> can be converted to
>>
>> | Col 1 | Col 2 | Col 3 |

> I'd do this using an external language that has a matrix/table data type
> with column indexing.

Well, in the end I just used Excel :">

-- 

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Sorting table columns (*not* content)
  2010-11-01 19:44     ` Carsten Dominik
@ 2010-11-02 11:05       ` Gary
  0 siblings, 0 replies; 14+ messages in thread
From: Gary @ 2010-11-02 11:05 UTC (permalink / raw)
  To: emacs-orgmode

Carsten Dominik wrote:
> On Nov 1, 2010, at 3:15 PM, Gary wrote:
>
>> Jeff Horn wrote:
>>
>>> Hope this helps,
>>
>> Not much, really. You're saying that I can do rectangle editing, which
>> I
>> knew. Are you basically saying "you have to sort them yourself"? (If
>> so,
>> that's okay, it's a valid answer, even if it isn't the one I want!)
>
> I guess in many cases it will be fast to sort by hand in a situation
> like this.

Depends how many columns you have. I had constructed a table whose
header contained variable references. Wiiiiiiiide table. I wanted to
sort to find the same named variables (and remove duplicates).

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2010-11-02 13:34 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-01 13:42 Sorting table columns (*not* content) Gary
2010-11-01 13:52 ` Jeff Horn
2010-11-01 14:15   ` Gary
2010-11-01 15:09     ` Jeff Horn
2010-11-01 19:44     ` Carsten Dominik
2010-11-02 11:05       ` Gary
2010-11-01 19:02   ` Christian Moe
2010-11-01 19:27     ` brian powell
2010-11-01 14:43 ` Juan Pechiar
2010-11-01 15:33   ` Gary
2010-11-01 15:39   ` brian powell
2010-11-01 17:47 ` Dan Davison
2010-11-01 18:44   ` brian powell
2010-11-02 11:03   ` Gary

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).