0

Estoy iniciándome en python y tengo problemas para ordenar el siguiente array de strings

["DG1000420,2013-12-01,36.719997,36.18,35.639999",
"DG1000421,2013-12-01,36.719997,36.18,35.639999",
"DG1000422,2013-12-01,36.719997,36.18,35.639999",
"DG1000420,2013-12-02,32.399998,32.939999,32.399998",
"DG1000421,2013-12-02,32.399998,32.939999,32.399998",
"DG1000422,2013-12-02,32.399998,32.939999,32.399998",
"DG1000420,2013-12-03,53.819996,53.099998,52.739998",
"DG1000421,2013-12-03,53.819996,53.099998,52.739998",
"DG1000422,2013-12-03,53.819996,53.099998,52.739998",
"DG1000420,2013-12-04,52.019997,53.279999,50.939999",
"DG1000421,2013-12-04,52.019997,53.279999,50.939999",
"DG1000422,2013-12-04,52.019997,53.279999,50.939999",
"DG1000420,2013-12-05,54.359997,53.639999,53.819996",
"DG1000421,2013-12-05,54.359997,53.639999,53.819996",
"DG1000422,2013-12-05,54.359997,53.639999,53.819996",
"DG1000420,2013-12-06,51.839996,51.659996,52.199997",
"DG1000421,2013-12-06,51.839996,51.659996,52.199997",
"DG1000422,2013-12-06,51.839996,51.659996,52.199997",
"DG1000420,2013-12-07,49.139999,47.879997,46.619999",
"DG1000421,2013-12-07,49.139999,47.879997,46.619999",
"DG1000422,2013-12-07,49.139999,47.879997,46.619999",
"DG1000420,2013-12-08,38.879997,37.799999,38.34",
"DG1000421,2013-12-08,38.879997,37.9999,38.34",
"DG1000422,2013-12-08,38.879997,37.799999,38.34"]

Me gustaria que el nuevo array quedara ordenado por el primer valor del string, tal que:

["DG1000420,2013-12-01,36.719997,36.18,35.639999",
"DG1000420,2013-12-02,32.399998,32.939999,32.399998",
"DG1000420,2013-12-03,53.819996,53.099998,52.739998",

He intentando usando collections.OrderedDict pero me ha sido imposible.

Alguna idea?

Gracias de antemano

2
  • 1
    por favor muestra exactamente qué intentaste, para poder empezar a partir de allí. Por otra parte, siempre es útil un ejemplo mínimo para que quede más claro que con muchísimas líneas
    – fedorqui
    Commented el 15 abr. 2020 a las 7:39
  • Es muy importante lo que comenta fedorqui, creo que lo que realmente quieres es ordenar teniendo en cuenta el contenido de la cadena hasta la primera coma (DG1000420, DG1000421, etc). Aún así queda una duda, ante DG100 y DG23 por ejemplo cual va primero, si ordenamos lexicográficamente es DG100 pero si tenemos en cuenta el valor numérico por separado sería DG23... No se si esta última situación puede darse (longitud diferente) pero conviene aclararlo. Por cierto, parece un csv, ¿por qué no o parseas directamente para separar columnas?
    – FJSevilla
    Commented el 15 abr. 2020 a las 10:29

1 respuesta 1

0

Prueba a ordenar con sort.

array.sort();

print(array);

Resultado

['DG1000420,2013-12-01,36.719997,36.18,35.639999', 'DG1000420,2013-12-02,32.399998,32.939999,32.399998', 'DG1000420,2013-12-03,53.819996,53.099998,52.739998', 'DG1000420,2013-12-04,52.019997,53.279999,50.939999', 'DG1000420,2013-12-05,54.359997,53.639999,53.819996', 'DG1000420,2013-12-06,51.839996,51.659996,52.199997', 'DG1000420,2013-12-07,49.139999,47.879997,46.619999', 'DG1000420,2013-12-08,38.879997,37.799999,38.34', 'DG1000421,2013-12-01,36.719997,36.18,35.639999', 'DG1000421,2013-12-02,32.399998,32.939999,32.399998', 'DG1000421,2013-12-03,53.819996,53.099998,52.739998', 'DG1000421,2013-12-04,52.019997,53.279999,50.939999', 'DG1000421,2013-12-05,54.359997,53.639999,53.819996', 'DG1000421,2013-12-06,51.839996,51.659996,52.199997', 'DG1000421,2013-12-07,49.139999,47.879997,46.619999', 'DG1000421,2013-12-08,38.879997,37.9999,38.34', 'DG1000422,2013-12-01,36.719997,36.18,35.639999', 'DG1000422,2013-12-02,32.399998,32.939999,32.399998', 'DG1000422,2013-12-03,53.819996,53.099998,52.739998', 'DG1000422,2013-12-04,52.019997,53.279999,50.939999', 'DG1000422,2013-12-05,54.359997,53.639999,53.819996', 'DG1000422,2013-12-06,51.839996,51.659996,52.199997', 'DG1000422,2013-12-07,49.139999,47.879997,46.619999', 'DG1000422,2013-12-08,38.879997,37.799999,38.34']
2
  • No me imaginaba que fuera tan facil. Ordena correctamente cada uno de los arrays por el primer valor del string. Gracias
    – Raul A.R
    Commented el 17 abr. 2020 a las 2:46
  • @RaulA.R para eso estamos ;). Si consideras la pregunta resuelta márcala para quede como respondida.
    – Carmen
    Commented el 17 abr. 2020 a las 18:53

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.