Sort list of strings with data in the middle of them
<p>So I have a list of strings which correlates to kibana indices, the strings look like this: </p>

<pre><code>Î» curl '10.10.43.210:9200/_cat/indices?v'
health status index               pri rep docs.count docs.deleted store.size pri.store.size
yellow open   filebeat-2016.10.08   5   1        899            0    913.8kb        913.8kb
yellow open   filebeat-2016.10.12   5   1        902            0    763.9kb        763.9kb
yellow open   filebeat-2016.10.13   5   1        816            0    588.9kb        588.9kb
yellow open   filebeat-2016.10.10   5   1        926            0    684.1kb        684.1kb
yellow open   filebeat-2016.10.11   5   1        876            0    615.2kb        615.2kb
yellow open   filebeat-2016.10.09   5   1        745            0    610.7kb        610.7kb
</code></pre>

<p>The dates are coming back unsorted.  I want to sort these by the index (which is a date) filebeat-2016-10.xx ASC or DESC is fine.    </p>

<p>As it stands now I isolate the strings like this:</p>

<pre><code>    subp = subprocess.Popen(['curl','-XGET' ,'-H', '"Content-Type: application/json"', '10.10.43.210:9200/_cat/indices?v'], stdout=subproce$
    curlstdout, curlstderr = subp.communicate()
    op = str(curlstdout)
    kibanaIndices = op.splitlines()
    for index,elem in enumerate(kibanaIndices):
            if "kibana" not in kibanaIndices[index]:
                    print kibanaIndices[index]+"\n"
                    kibanaIndexList.append(kibanaIndices[index])
</code></pre>

<p>But can't sort them in a meaningful way.</p>

<p>I copied your sample data into a text file as UTF-8 since I don't have access to the server you referenced. Using list comprehensions and string methods you can clean the data then break it down into its component parts. Sorting is accomplished by passing a lambda function as an argument to the builtin sorted() method:</p>

<pre><code># read text data into list one line at a time
result_lines = open('kibana_data.txt').readlines()

# remove trailing newline characters
clean_lines = [line.replace("\n", "") for line in result_lines]

# get first line of file
info = clean_lines[0]

# create list of field names
header = [val.replace(" ", "") 
          for val in clean_lines[1].split()]

# create list of lists for data rows
data = [[val.replace(" ", "") for val in line.split()] 
        for line in clean_lines[2:]]

# sort data rows by date (third row item at index 2)
final = sorted(data, key=lambda row: row[2], reverse=False)
</code></pre>

<p>More info on list comprehensions here: <a href="https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions" rel="nofollow">https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions</a></p>

<p>More info on sorting here: <a href="https://wiki.python.org/moin/HowTo/Sorting" rel="nofollow">https://wiki.python.org/moin/HowTo/Sorting</a></p>

python sorting