#Sort the columns of a csv-file

You get a string with the content of a csv-file. The columns are separated by semicolons.
The first line contains the names of the columns.
Write a method that sorts the columns by the names of the columns alphabetically and incasesensitive. 

An example:
```
Before sorting:
As table (only visualization):
|myjinxin2015|raulbc777|smile67|Dentzil|SteffenVogel_79|
|17945       |10091    |10088  |3907   |10132          |
|2           |12       |13     |48     |11             |

The csv-file:
myjinxin2015;raulbc777;smile67;Dentzil;SteffenVogel_79\n
17945;10091;10088;3907;10132\n
2;12;13;48;11

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

After sorting:
As table (only visualization):
|Dentzil|myjinxin2015|raulbc777|smile67|SteffenVogel_79|
|3907   |17945       |10091    |10088  |10132          |
|48     |2           |12       |13     |11             |

The csv-file:
Dentzil;myjinxin2015;raulbc777;smile67;SteffenVogel_79\n
3907;17945;10091;10088;10132\n
48;2;12;13;11
```

There is no need for prechecks. You will always get a correct string with more than 1 line und more than 1 row. All columns will have different names.

Have fun coding it and please don't forget to vote and rank this kata! :-) 

I have created other katas. Have a look if you like coding and challenges.

Source: codewars | Difficulty: EASY
Tags: String algorithms

Sorting

Data structures

Starter code: def sort_csv_columns(csv_file_content):
	

Solutions:
def sort_csv_columns(csv_file_content, sep=';', end='\n'):
	csv_columns = zip(*(row.split(sep) for row in csv_file_content.split(end)))
	sorted_columns = sorted(csv_columns, key=lambda col: col[0].lower())
	return end.join((sep.join(row) for row in zip(*sorted_columns)))


def sort_csv_columns(csv_file_content):
	return '\n'.join((';'.join(sorted_row) for sorted_row in zip(*sorted(zip(*(row.split(';') for row in csv_file_content.split('\n'))), key=lambda col: col[0].lower()))))


def sort_csv_columns(csv_file_content):
	array = (line.split(';') for line in csv_file_content.split('\n'))
	trans = list(zip(*array))
	sortd = sorted(trans, key=lambda col: col[0].lower())
	untrans = list(zip(*sortd))
	return '\n'.join((';'.join(line) for line in untrans))


SEP = ';'

def sort_csv_columns(csv):
	names = csv.split('\n', 1)[0].lower().split(SEP)
	d = {names.index(c): i for (i, c) in enumerate(sorted(names))}
	return '\n'.join((SEP.join((e[1] for e in sorted([(d[i], field) for (i, field) in enumerate(row.split(SEP))]))) for row in csv.split('\n')))


def sort_csv_columns(csv_file_content):
	rows = csv_file_content.split('\n')
	titles = rows[0].split(';')
	order = sorted(range(len(titles)), key=lambda k: titles[k].lower())
	newpages = []
	for row in rows:
		newpages.append(';'.join([row.split(';')[c] for c in order]))
	return '\n'.join(newpages)


from csv import reader

def sort_csv_columns(csv_file_content):
	return '\n'.join(map(';'.join, zip(*sorted(zip(*reader(csv_file_content.split('\n'), delimiter=';')), key=lambda x: x[0].lower()))))


def sort_csv_columns(csv):
	s = ''
	res = sorted(zip(*[x.split(';') for x in csv.split('\n')]), key=lambda x: x[0].lower())
	for i in range(len(res[-1])):
		s += ';'.join((x[i] for x in res)) + '\n'
	return s.strip('\n')


from collections import defaultdict

def sort_csv_columns(s):
	d = defaultdict(list)
	for i in s.splitlines():
		for (j, k) in enumerate(i.split(';')):
			d[j].append(k)
	r = zip(*[i[1] for i in sorted(d.items(), key=lambda x: (x[1][0].lower(), x[1][0]))])
	return '\n'.join([';'.join(i) for i in r])


import string

def sort_csv_columns(csv_file_content):
	orderby = ' _0123456789' + string.ascii_lowercase
	l = list(zip(*[i.split(';') for i in csv_file_content.split('\n')]))
	l.sort(key=lambda a: [orderby.index(i) for i in a[0].lower()])
	return '\n'.join((';'.join(i) for i in zip(*l)))