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>Is it what you need?</p>

<pre><code>lines = """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
""".splitlines()
def extract_date(line):
    return line.split()[2]
lines.sort(key=extract_date)
print("\n".join(lines))
</code></pre>

<p>Here <code>extract_date</code> is a function that returns third column (like <code>filebeat-2016.10.12</code>). We use this function as <code>key</code> argument to <code>sort</code> to use this value as a sort key. You date format can be sorted just as strings. You can probably use more sophisticated <code>extract_line</code> function to extract only the date.</p>

python sorting