sorting file content by third numbered column

33 views
Skip to first unread message

Ni Va

unread,
Mar 12, 2018, 4:50:41 PM3/12/18
to vim_use
Hi,

I don't happen to sort this kind of list by the third float numbered column.

Foo..ctor() 1 10.4901 10.4901
Bar() 2 0.132 0.066
FooBar.set_foo() 173 6.6418 0.038392
Foo.set_bar() 2 0.2234 0.1117
FooBar.Calc() 358 1164.8912 3.253886


Timothy Rice

unread,
Mar 12, 2018, 5:19:41 PM3/12/18
to vim...@googlegroups.com
Personally, I would pipe it through an external command, eg:

:%! sort -n -k3

Within Vim on a Linux box that command results in text roughly like so for
me:

Bar() 2 0.132 0.066
Foo.set_bar() 2 0.2234 0.1117
FooBar.set_foo() 173 6.6418 0.038392
Foo..ctor() 1 10.4901 10.4901
FooBar.Calc() 358 1164.8912 3.253886

(I've manually inserted some space for readability reasons.)

~ Tim
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ni Va

unread,
Mar 12, 2018, 9:31:27 PM3/12/18
to vim_use
Thanks but it's a list content not a buffer content in my case.

Christian Brabandt

unread,
Mar 13, 2018, 7:18:31 AM3/13/18
to vim_use

On Mo, 12 Mär 2018, Ni Va wrote:

> Thanks but it's a list content not a buffer content in my case.

Write a custom Sort Function, that splits() your content and sorts on
the third field?

BTW: Please provide complete exact examples. Makes it easier to
understand you and test.

Best,
Christian
--
Fast alles, was Menschen erfinden, wird früher oder später zur Waffe.
Ist das wirklich nicht zu verhindern?
-- Linus Pauling

Ni Va

unread,
Mar 13, 2018, 9:30:19 AM3/13/18
to vim_use
By Total time column for example.
2018_03_09_11_39_51.981_TracesProfilage_1.csv

Christian Brabandt

unread,
Mar 13, 2018, 9:34:20 AM3/13/18
to vim_use

On Di, 13 Mär 2018, Ni Va wrote:

> By Total time column for example.

Sorry, I find your answers in general too terse to try to figure out
what exactly you are trying to do now and I do not feel like guessing.

It looks as you'd like to sort buffer content, while you were saying
before you want to sort lists.

Oh and BTW: please trim your quotes and write your statement below to
what you are referring to.

Thanks,
Christian
--
Der gesunde Menschenverstand ist blind sowohl für das äußerst Böse
wie für das höchst Gute.
-- Karl Jaspers

Ni Va

unread,
Mar 13, 2018, 10:41:47 AM3/13/18
to vim_use
The csv file attached below it produced from this code particularly by from s:results list of strings.

I import it into excel and then do a sort from max to min spent time by third column.

I would do the same from this vimscript below on in order to sort by numeric float Total time column



fun! helper#ybereportstats_timebyfunc(list) "{{{


"2. Total time by function
"
let totaltimelist = filter(copy(a:list), 'v:val =~ "TOTAL"')
let pattime = '\(\d\+\:\d\+:\d\+\.\d\+\)'
let patfunc = '\(\S\+()\)'
let pattotaltime = '\(\d\+\(,\d\+\)\{0,1}\)'
let linepat = '^.\{-}'.pattime.'.\{-}'.patfunc.'.\{-}'.pattotaltime.'.\{-}$'
let listtotaltimespent = map(copy(totaltimelist)[0:s:nEch], 'substitute(v:val, linepat, "\\2@\\3@\\1", "")')
" echomsg string(listtotaltimespent)
" return

for value in listtotaltimespent
" k : class/func name
" v : total time for the call..
" t : ..at time
" try
let [k,v,t] = split(value,'@')
" catch /.*/
" echomsg 'error ' . string(value)
" endtry
if !has_key(s:dict,k)
let s:dict[k] = { 'values' : [{'totaltime':v, 'time':t}] }
else
call extend(s:dict[k].values, [{'totaltime':v, 'time':t}])
endif
endfor
" echo s:dict

"
" 2. build results
"
let s:results = [join(["Func","Nb Occs", "Total time (ms)","Indicateur (ms/ech)"],s:sep)]
for funclass in keys(s:dict)
" Sum up total time for N occurs
let nOccur = len(s:dict[funclass].values)

" Sum up total time for N occurs
let sum=0.0
for time in s:dict[funclass].values
" echomsg time.totaltime
let sum+=str2float(substitute(time.totaltime, ',', '.', 'g'))
endfor

" let sumstr = tr(printf("%s",sum),'.',',')
let sumstr = printf("%s",sum)

let indicateur=printf("%s",sum/nOccur)

call extend(s:results, [funclass.s:sep.string(nOccur).s:sep.sumstr.s:sep.indicateur])
endfor
" echomsg string(s:results)
endfunction "}}}

Erik Christiansen

unread,
Mar 14, 2018, 3:17:53 AM3/14/18
to vim_use
On 13.03.18 07:41, Ni Va wrote:
> The csv file attached below it produced from this code particularly by from s:results list of strings.
>
> I import it into excel and then do a sort from max to min spent time by third column.
>
> I would do the same from this vimscript below on in order to sort by numeric float Total time column

[Miles long inscrutably complex vimscript elided.]

Vim is not emacs, so it does not burden itself with diverse
functionality in the same way - and yet it has an inbuilt sort. OK, that
apparently does not suffice for your requirements.

Then the task is much more efficiently executed by existing tools
designed for it. Have a look at "man sort". In a single short command
line, you can replace all the wasteful complexity of a miles long
inscrutable vimscript, instead handling which field to sort on, field
separator, forward/reverse sort, numeric or alpha, etc. I repeat, one
short line.

A hammer can be used as a screwdriver, but only if you treat the screw
as a nail. Results are likely to be disappointing.

Erik

--
I have yet to see any problem, however complicated, which, when you
looked at it in the right way, did not become still more complicated.
- Poul Anderson

Christian Brabandt

unread,
Mar 14, 2018, 3:54:02 AM3/14/18
to vim_use

On Di, 13 Mär 2018, Ni Va wrote:

> > Oh and BTW: please trim your quotes and write your statement below to
> > what you are referring to.

Please trim your quotes.

Have you tried using a custom compare function similar to what is
written to the help at :h sort()?

BTW: please provide a minimal working example of what problem you are
trying to solve. I am not trying to debug your complex script. Sorry.


Best,
Christian
--
Es ist nicht genug, der Natur die Daumenschrauben anzulegen.
Man muß auch hinhören, wenn sie aussagt.
-- Arthur Schopenhauer

Ni Va

unread,
Mar 14, 2018, 4:31:24 AM3/14/18
to vim...@googlegroups.com
I have done this explained before. Dict that have total time in key and csv line in value.

Then I can do a  reverse(sort( numerical order but I would like to retrieve values and the code above retrieve keys sorted.....
so I need the last step.

.
.
   let line = funclass.s:sep.string(nOccur).s:sep.sumstr.s:sep.indicateur
   call extend(s:sorted_by_total_time, {sumstr : line})

  endfor
 
let s:results_by_total_time = reverse(sort(keys(s:sorted_by_total_time),'N'))




--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/rjR-W-7MQNc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+unsubscribe@googlegroups.com.

Ni Va

unread,
Mar 14, 2018, 4:53:50 AM3/14/18
to vim_use
> To unsubscribe from this group and all its topics, send an email to vim_use+u...@googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Would like to use map( but this seems to do the job.

let s:results_by_total_time = []
for key in reverse(sort(keys(copy(s:sorted_by_total_time )),'N'))
call add(s:results_by_total_time, s:sorted_by_total_time[key])
endfor

Thank you.
Reply all
Reply to author
Forward
0 new messages