0

Example:

let list = [['aen', '2'], ['ben', '3'], ['Aen', '4'], ['Ben', '5']]

sort(list, 1)

Output:

let list = [['aen', '2'], ['Aen', '4'], ['ben', '3'], ['Ben', '5']]

Expected output:

let list = [['Aen', '4'], ['aen', '2'], ['Ben', '5'], ['ben', '3']]

In python that would be easy to do but it seems not te be possible to use the python sorted() command with a vim list.

How can I sort the list as expected?

UPDATE:

Maybe a solution would be:

for i in range(0,len(list)-1) 
  let @b= join(list[i], "|||") 
endfor

This would put all the lines in a register but how can I sort the lines in a register?

EDIT: Found a solution with the help of python (within Vim Function)

python3 << endpython
import vim
list2 = vim.eval('list')
list3 = sorted(list2, key=lambda v: (v[0].upper(), v[0].islower()))
vim.command("let list= %s"% list3)
endpython

echo list --> [['Aen', '4'], ['aen', '2'], ['Ben', '5'], ['ben', '3']]
Reman
  • 7,931
  • 11
  • 55
  • 97

3 Answers3

1

Reman: Found a solution with the help of python (within Vim Function)

python3 << endpython
import vim
list2 = vim.eval('list')
list3 = sorted(list2, key=lambda v: (v[0].upper(), v[0].islower()))
vim.command("let list= %s"% list3)
endpython

echo list --> [['Aen', '4'], ['aen', '2'], ['Ben', '5'], ['ben', '3']]
Armali
  • 18,255
  • 14
  • 57
  • 171
0

sort() takes an optional parameter which is a function. Define your function that return -1, 0, or 1 depending on the first element of your sub-lists.

Note that you'll have to define the function for your altered lexical order.

May be instead, you could sort the transposed versions with tr(), and then transpose back after the sort.

Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
0

I think to obtain your expected result in python, you have to write your own compare function too. In vim, you can first impl. the compare function, and pass the function ref to the sort() function.

Please check the :h sort( for detail

Kent
  • 189,393
  • 32
  • 233
  • 301
  • Hello Kent. I have no idea how to import a vim list in python command. In python you can do it if I'm not wrong with a combination of `key=s.lower` and `key=itemgetter(0)`. Don't know either how to write a compare function in vim, I will consult the help file. – Reman Feb 05 '16 at 10:47
  • in python `key=str.lower` won't give what you wanted. `A a B b` . – Kent Feb 05 '16 at 13:32
  • 1
    and if your question is how to transfer variable/list from py to vim, you've asked another question, and I posted an answer. Maybe you can check it again: http://stackoverflow.com/questions/17656320/using-python-in-vimscript-how-to-export-a-value-from-a-python-script-back-to-vi – Kent Feb 05 '16 at 13:42
  • no my question is about sorting a (nested) list. I still have no idea how to do it. There is an example in vim help files `MyCompare()` if there are only numbers in my list but even here.. I don't know how to use it with a nested list. Not for this question. I will ask another one. – Reman Feb 05 '16 at 13:55
  • @Reman nested list is not a problem. Because the key is the first element (the str). What you want to do is sort the list by the key. The real problem is to sort list `['a','A','b','B']` into `['A','a','B','b']` I think you need write your own compFunc anyway, no matter in python or vim – Kent Feb 05 '16 at 13:59
  • Do you think the updated part of my question can be a solution? – Reman Feb 05 '16 at 14:02
  • @Reman I didn't see the sorting implementation in your edit. again, you need impl. your own compFunction. – Kent Feb 05 '16 at 14:04
  • @I'm trying to find other solutions because I really don't have any idea how to create my own compFunction i.e. how to sort the list from `['a','A','b','B']` into `['A','a','B','b']` – Reman Feb 05 '16 at 14:06
  • Found it in python: `sorted(list, key=lambda v: (v[0].upper(), v[0].islower()))` but now... how to import list[] in python and export list[] again to vim. Any help please :) – Reman Feb 05 '16 at 16:46