1
$\begingroup$

I want to sort the following nested list of peak points by the y values while keeping the dimensions consistent.

I then want to extract the x value of the largest and second largest y values (if the dimension is 1 then the only item of the list should be the largest but I would also like to extract it as one of the second largest group too).

Any help is greatly appreciated!

listOfPeaks = {{{1050, 0.0181119}, {1281, 0.023783}, {1773, 
     0.32029}, {2035, 0.0233451}, {2167, 0.0159332}}, {{1053, 
     0.0173196}, {1282, 0.0229119}, {1773, 0.326619}, {2033, 
     0.022375}, {2165, 0.0152391}}, {{1056, 0.0166205}, {1283, 
     0.0221355}, {1773, 0.332107}, {2032, 0.021521}, {2164, 
     0.0146316}}, {{1058, 0.0159982}, {1284, 0.0214385}, {1772, 
     0.336923}, {2031, 0.0207607}, {2162, 0.014094}}, {{1061, 
     0.0154399}, {1285, 0.0208085}, {1772, 0.341195}, {2030, 
     0.020078}, {2160, 0.0136136}}, {{1063, 0.0149354}, {1286, 
     0.0202355}, {1772, 0.345011}, {2030, 0.0194612}, {2159, 
     0.013181}}, {{1065, 0.0144767}, {1287, 0.0197113}, {1772, 
     0.348445}, {2029, 0.0188999}, {2158, 0.0127885}}, {{1067, 
     0.0140573}, {1288, 0.0192295}, {1772, 0.351557}, {2028, 
     0.0183859}, {2156, 0.0124305}}, {{1069, 0.0136721}, {1288, 
     0.0187846}, {1772, 0.354392}, {2027, 0.0179127}, {2155, 
     0.012102}}, {{1070, 0.0133166}, {1289, 0.0183724}, {1772, 
     0.356988}, {2027, 0.0174762}, {2154, 0.0117991}}, {{1072, 
     0.0129872}, {1290, 0.0179887}, {1771, 0.359379}, {2026, 
     0.017071}, {2153, 0.0115187}}, {{1073, 0.0126809}, {1290, 
     0.0176304}, {1771, 0.361589}, {2026, 0.0166932}, {2152, 
     0.0112581}}, {{1075, 0.0123953}, {1291, 0.0172952}, {1771, 
     0.363639}, {2025, 0.0163411}, {2150, 0.0110151}}, {{1076, 
     0.0121279}, {1292, 0.01698}, {1771, 0.365546}, {2025, 
     0.0160102}, {2149, 0.0107877}}, {{1078, 0.011877}, {1292, 
     0.0166836}, {1771, 0.367326}, {2024, 0.0157005}, {2149, 
     0.0105743}}, {{1079, 0.011641}, {1293, 0.0164036}, {1771, 
     0.368993}, {2024, 0.0154075}, {2148, 0.0103737}}, {{1080, 
     0.0114185}, {1293, 0.016139}, {1771, 0.370557}, {2023, 
     0.0151321}, {2147, 0.0101844}}, {{1081, 0.0112081}, {1294, 
     0.015888}, {1771, 0.372028}, {2023, 0.0148706}, {2146, 
     0.0100055}}, {{1082, 0.011009}, {1294, 0.0156499}, {1771, 
     0.373416}, {2022, 0.0146232}}, {{1084, 0.01082}, {1295, 
     0.015423}, {1771, 0.374727}, {2022, 0.0143881}}, {{1085, 
     0.0106404}, {1295, 0.0152073}, {1770, 0.375969}, {2021, 
     0.0141639}}, {{1086, 0.0104695}, {1295, 0.015001}, {1770, 
     0.377149}, {2021, 0.0139511}}, {{1087, 0.0103065}, {1296, 
     0.0148042}, {1770, 0.378271}, {2021, 0.0137472}}, {{1088, 
     0.0101508}, {1296, 0.0146157}, {1770, 0.379338}, {2020, 
     0.0135526}}, {{1089, 0.010002}, {1297, 0.014435}, {1770, 
     0.380355}, {2020, 0.0133666}}, {{1297, 0.0142619}, {1770, 
     0.381326}, {2020, 0.0131879}}, {{1297, 0.0140953}, {1770, 
     0.382253}, {2019, 0.0130165}}, {{1298, 0.0139352}, {1770, 
     0.383141}, {2019, 0.0128523}}, {{1298, 0.0137813}, {1770, 
     0.383992}, {2019, 0.012694}}, {{1298, 0.0136328}, {1770, 
     0.384808}, {2018, 0.0125413}}, {{1299, 0.0134895}, {1770, 
     0.385592}, {2018, 0.012395}}, {{1299, 0.0133515}, {1770, 
     0.386345}, {2018, 0.0122535}}, {{1299, 0.013218}, {1770, 
     0.387069}, {2018, 0.0121166}}, {{1299, 0.0130888}, {1770, 
     0.387767}, {2017, 0.0119846}}, {{1300, 0.012964}, {1770, 
     0.38844}, {2017, 0.0118573}}, {{1300, 0.0128431}, {1770, 
     0.389089}, {2017, 0.0117339}}, {{1300, 0.0127259}, {1770, 
     0.389715}, {2017, 0.0116141}}, {{1301, 0.0126121}, {1770, 
     0.39032}, {2016, 0.0114981}}, {{1301, 0.012502}, {1769, 
     0.390906}, {2016, 0.0113861}}, {{1301, 0.0123951}, {1769, 
     0.391474}, {2016, 0.0112773}}, {{1301, 0.012291}, {1769, 
     0.392024}, {2016, 0.0111714}}, {{1301, 0.0121898}, {1769, 
     0.392557}, {2016, 0.0110683}}, {{1302, 0.0120917}, {1769, 
     0.393073}, {2015, 0.0109687}}, {{1302, 0.0119961}, {1769, 
     0.393574}, {2015, 0.0108718}}, {{1302, 0.011903}, {1769, 
     0.39406}, {2015, 0.0107774}}, {{1302, 0.0118122}, {1769, 
     0.394532}, {2015, 0.0106853}}, {{1303, 0.0117238}, {1769, 
     0.394991}, {2015, 0.0105955}}, {{1303, 0.0116378}, {1769, 
     0.395437}, {2014, 0.0105083}}, {{1303, 0.0115538}, {1769, 
     0.395871}, {2014, 0.0104235}}, {{1303, 0.0114718}, {1769, 
     0.396294}, {2014, 0.0103407}}, {{1303, 0.0113916}, {1769, 
     0.396705}, {2014, 0.0102597}}, {{1304, 0.0113135}, {1769, 
     0.397105}, {2014, 0.0101805}}, {{1304, 0.0112372}, {1769, 
     0.397495}, {2013, 0.0101031}}, {{1304, 0.0111627}, {1769, 
     0.397876}, {2013, 0.0100281}}, {{1304, 0.0110897}, {1769, 
     0.398247}}, {{1304, 0.0110183}, {1769, 0.398609}}, {{1305, 
     0.0109484}, {1769, 0.398962}}, {{1305, 0.0108802}, {1769, 
     0.399307}}, {{1305, 0.0108134}, {1769, 0.399644}}, {{1305, 
     0.010748}, {1769, 0.399973}}, {{1305, 0.0106838}, {1769, 
     0.400295}}, {{1305, 0.0106209}, {1769, 0.40061}}, {{1306, 
     0.0105592}, {1769, 0.400917}}, {{1306, 0.0104989}, {1769, 
     0.401219}}, {{1306, 0.0104398}, {1769, 0.401513}}, {{1306, 
     0.0103817}, {1769, 0.401802}}, {{1306, 0.0103247}, {1769, 
     0.402084}}, {{1306, 0.0102688}, {1769, 0.402361}}, {{1306, 
     0.0102138}, {1769, 0.402632}}, {{1307, 0.0101599}, {1769, 
     0.402897}}, {{1307, 0.010107}, {1769, 0.403158}}, {{1307, 
     0.0100551}, {1768, 0.403414}}, {{1307, 0.010004}, {1768, 
     0.403665}}, {{1768, 0.403911}}, {{1768, 0.404153}}, {{1768, 
     0.40439}}, {{1768, 0.404623}}, {{1768, 0.404852}}, {{1768, 
     0.405077}}, {{1768, 0.405297}}, {{1768, 0.405514}}, {{1768, 
     0.405727}}, {{1768, 0.405937}}, {{1768, 0.406143}}, {{1768, 
     0.406345}}, {{1768, 0.406544}}, {{1768, 0.40674}}, {{1768, 
     0.406932}}, {{1768, 0.407122}}, {{1768, 0.407308}}, {{1768, 
     0.407492}}};

Map[Length, listOfPeaks, {1}]

```
$\endgroup$
3
$\begingroup$

If I understood correctly:

tab = SortBy[#, Last] & /@ listOfPeaks
Table[If[Length@t == 1, {t[[-1]], t[[-1]]}, {t[[-1]], t[[-2]]}], {t, tab}]

First it sorts each sublist by the last element (the $y$ value), then it iterates and pick twice the last element if the element is a List of length 1, or the two lasts otherwise.

$\endgroup$
1
  • $\begingroup$ Thanks for your help, this is what I was looking for! $\endgroup$ – James Feb 10 '20 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.