Would appreciate assistance in returning only a Nth item after sorting in desc order

Describe the issue/error/question

I have a large amount of data output but only require a specific item after sorting in descenting order.

What is the error message (if any)?

Please share the workflow

(Select the nodes and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow respectively)

{
“meta”: {
“instanceId”: “3b4f5dc05fe576016b69b65bb579b15d215a729edd26875172032953dda064c6”
},
“nodes”: [
{
“parameters”: {
“functionCode”: “return [\n {\n "data": [\n {\n "result": [\n {\n "itemid": "1259108",\n "clock": "1664056808",\n "value": "5327944",\n "ns": "80459833"\n },\n {\n "itemid": "1259108",\n "clock": "1664056988",\n "value": "6381024",\n "ns": "416154677"\n },\n {\n "itemid": "1259108",\n "clock": "1664057168",\n "value": "5994344",\n "ns": "34277185"\n },\n {\n "itemid": "1259108",\n "clock": "1664057348",\n "value": "5093944",\n "ns": "610728776"\n },\n {\n "itemid": "1259108",\n "clock": "1664057528",\n "value": "5615624",\n "ns": "119916715"\n },\n {\n "itemid": "1259108",\n "clock": "1664057708",\n "value": "7281832",\n "ns": "39228871"\n },\n {\n "itemid": "1259108",\n "clock": "1664057888",\n "value": "5754832",\n "ns": "402907498"\n },\n {\n "itemid": "1259108",\n "clock": "1664058068",\n "value": "4691336",\n "ns": "29565254"\n },\n {\n "itemid": "1259108",\n "clock": "1664058248",\n "value": "3993040",\n "ns": "212976662"\n },\n {\n "itemid": "1259108",\n "clock": "1664058428",\n "value": "4275416",\n "ns": "211985471"\n },\n {\n "itemid": "1259108",\n "clock": "1664058608",\n "value": "3566624",\n "ns": "65279763"\n },\n {\n "itemid": "1259108",\n "clock": "1664058788",\n "value": "4303688",\n "ns": "130166375"\n },\n {\n "itemid": "1259108",\n "clock": "1664058968",\n "value": "4501688",\n "ns": "31208855"\n },\n {\n "itemid": "1259108",\n "clock": "1664059148",\n "value": "3757296",\n "ns": "180437929"\n },\n {\n "itemid": "1259108",\n "clock": "1664059328",\n "value": "5444688",\n "ns": "477077490"\n },\n {\n "itemid": "1259108",\n "clock": "1664059508",\n "value": "4504176",\n "ns": "350184522"\n },\n {\n "itemid": "1259108",\n "clock": "1664059688",\n "value": "3916208",\n "ns": "201505724"\n },\n {\n "itemid": "1259108",\n "clock": "1664059868",\n "value": "4725408",\n "ns": "12802423"\n },\n {\n "itemid": "1259108",\n "clock": "1664060048",\n "value": "4297304",\n "ns": "113636165"\n },\n {\n "itemid": "1259108",\n "clock": "1664060228",\n "value": "4443192",\n "ns": "38750663"\n },\n {\n "itemid": "1259108",\n "clock": "1664060408",\n "value": "5374392",\n "ns": "69202351"\n },\n {\n "itemid": "1259108",\n "clock": "1664060588",\n "value": "8052568",\n "ns": "67655184"\n },\n {\n "itemid": "1259108",\n "clock": "1664060768",\n "value": "5191496",\n "ns": "6275311"\n },\n {\n "itemid": "1259108",\n "clock": "1664060948",\n "value": "5260336",\n "ns": "602190488"\n },\n {\n "itemid": "1259108",\n "clock": "1664061128",\n "value": "4673408",\n "ns": "108091201"\n },\n {\n "itemid": "1259108",\n "clock": "1664061308",\n "value": "4651688",\n "ns": "573840857"\n },\n {\n "itemid": "1259108",\n "clock": "1664061488",\n "value": "4439992",\n "ns": "722896806"\n },\n {\n "itemid": "1259108",\n "clock": "1664061668",\n "value": "5449992",\n "ns": "293968948"\n },\n {\n "itemid": "1259108",\n "clock": "1664061848",\n "value": "3920440",\n "ns": "790466973"\n },\n {\n "itemid": "1259108",\n "clock": "1664062028",\n "value": "4091176",\n "ns": "288701099"\n },\n {\n "itemid": "1259108",\n "clock": "1664062208",\n "value": "7564600",\n "ns": "835244785"\n },\n {\n "itemid": "1259108",\n "clock": "1664062388",\n "value": "5871064",\n "ns": "290954931"\n },\n {\n "itemid": "1259108",\n "clock": "1664062568",\n "value": "6807600",\n "ns": "76654954"\n },\n {\n "itemid": "1259108",\n "clock": "1664062748",\n "value": "8242872",\n "ns": "330315171"\n },\n {\n "itemid": "1259108",\n "clock": "1664062928",\n "value": "6724048",\n "ns": "6773711"\n },\n {\n "itemid": "1259108",\n "clock": "1664063108",\n "value": "6819480",\n "ns": "5457814"\n },\n {\n "itemid": "1259108",\n "clock": "1664063288",\n "value": "8873688",\n "ns": "2065654"\n },\n {\n "itemid": "1259108",\n "clock": "1664063468",\n "value": "6078392",\n "ns": "6512459"\n },\n {\n "itemid": "1259108",\n "clock": "1664063648",\n "value": "6144576",\n "ns": "33059680"\n },\n {\n "itemid": "1259108",\n "clock": "1664063828",\n "value": "5551872",\n "ns": "470505033"\n },\n {\n "itemid": "1259108",\n "clock": "1664323208",\n "value": "13799040",\n "ns": "13486619"\n },\n {\n "itemid": "1259108",\n "clock": "1664323388",\n "value": "15668520",\n "ns": "555601197"\n },\n {\n "itemid": "1259108",\n "clock": "1664323568",\n "value": "12206200",\n "ns": "2473667"\n },\n {\n "itemid": "1259108",\n "clock": "1664323748",\n "value": "11556768",\n "ns": "33661825"\n },\n {\n "itemid": "1259108",\n "clock": "1664323928",\n "value": "13010504",\n "ns": "304893479"\n },\n {\n "itemid": "1259108",\n "clock": "1664324108",\n "value": "12318864",\n "ns": "7355906"\n },\n {\n "itemid": "1259108",\n "clock": "1664324288",\n "value": "9241144",\n "ns": "53594909"\n },\n {\n "itemid": "1259108",\n "clock": "1664324468",\n "value": "7427328",\n "ns": "501987791"\n },\n {\n "itemid": "1259108",\n "clock": "1664324648",\n "value": "8929936",\n "ns": "156890599"\n },\n {\n "itemid": "1259108",\n "clock": "1664324828",\n "value": "6719824",\n "ns": "3276976"\n },\n {\n "itemid": "1259108",\n "clock": "1664325008",\n "value": "6163936",\n "ns": "110762501"\n },\n {\n "itemid": "1259108",\n "clock": "1664325188",\n "value": "6239624",\n "ns": "535049853"\n },\n {\n "itemid": "1259108",\n "clock": "1664325368",\n "value": "7211392",\n "ns": "43485584"\n },\n {\n "itemid": "1259108",\n "clock": "1664325548",\n "value": "8579400",\n "ns": "26841142"\n },\n {\n "itemid": "1259108",\n "clock": "1664325728",\n "value": "6998688",\n "ns": "656969910"\n },\n {\n "itemid": "1259108",\n "clock": "1664325908",\n "value": "7612568",\n "ns": "482508977"\n },\n {\n "itemid": "1259108",\n "clock": "1664326088",\n "value": "6431896",\n "ns": "15082748"\n },\n {\n "itemid": "1259108",\n "clock": "1664326268",\n "value": "8095840",\n "ns": "109992243"\n },\n {\n "itemid": "1259108",\n "clock": "1664326448",\n "value": "7600888",\n "ns": "752853987"\n },\n {\n "itemid": "1259108",\n "clock": "1664326628",\n "value": "7133424",\n "ns": "582350589"\n },\n {\n "itemid": "1259108",\n "clock": "1664326808",\n "value": "6943568",\n "ns": "457386450"\n },\n {\n "itemid": "1259108",\n "clock": "1664326988",\n "value": "6988776",\n "ns": "145084372"\n },\n {\n "itemid": "1259108",\n "clock": "1664327168",\n "value": "6629192",\n "ns": "736188790"\n },\n {\n "itemid": "1259108",\n "clock": "1664327348",\n "value": "8358200",\n "ns": "85217495"\n },\n {\n "itemid": "1259108",\n "clock": "1664327528",\n "value": "39826384",\n "ns": "343450077"\n },\n {\n "itemid": "1259108",\n "clock": "1664327708",\n "value": "106183352",\n "ns": "90851531"\n },\n {\n "itemid": "1259108",\n "clock": "1664327888",\n "value": "103117736",\n "ns": "17178272"\n },\n {\n "itemid": "1259108",\n "clock": "1664328068",\n "value": "105164960",\n "ns": "320580587"\n },\n {\n "itemid": "1259108",\n "clock": "1664328248",\n "value": "108854848",\n "ns": "330765055"\n },\n {\n "itemid": "1259108",\n "clock": "1664328428",\n "value": "100004864",\n "ns": "2299471"\n },\n {\n "itemid": "1259108",\n "clock": "1664328608",\n "value": "106487016",\n "ns": "516362608"\n },\n {\n "itemid": "1259108",\n "clock": "1664328788",\n "value": "46252440",\n "ns": "550081413"\n },\n {\n "itemid": "1259108",\n "clock": "1664328968",\n "value": "11297736",\n "ns": "3487282"\n },\n {\n "itemid": "1259108",\n "clock": "1664329148",\n "value": "10741704",\n "ns": "547793754"\n },\n {\n "itemid": "1259108",\n "clock": "1664329328",\n "value": "7519696",\n "ns": "865273827"\n },\n {\n "itemid": "1259108",\n "clock": "1664329508",\n "value": "7160528",\n "ns": "27801100"\n },\n {\n "itemid": "1259108",\n "clock": "1664329688",\n "value": "8262888",\n "ns": "251313581"\n },\n {\n "itemid": "1259108",\n "clock": "1664329868",\n "value": "7877616",\n "ns": "2270784"\n },\n {\n "itemid": "1259108",\n "clock": "1664330048",\n "value": "7886416",\n "ns": "330934536"\n },\n {\n "itemid": "1259108",\n "clock": "1664330228",\n "value": "8351208",\n "ns": "8329610"\n },\n {\n "itemid": "1259108",\n "clock": "1664330408",\n "value": "8405512",\n "ns": "53285899"\n },\n {\n "itemid": "1259108",\n "clock": "1664330588",\n "value": "9398200",\n "ns": "89958468"\n },\n {\n "itemid": "1259108",\n "clock": "1664330768",\n "value": "13922512",\n "ns": "2353133"\n },\n {\n "itemid": "1259108",\n "clock": "1664330948",\n "value": "14578152",\n "ns": "506401830"\n },\n {\n "itemid": "1259108",\n "clock": "1664331128",\n "value": "14568600",\n "ns": "670140331"\n },\n {\n "itemid": "1259108",\n "clock": "1664331308",\n "value": "15660904",\n "ns": "4976993"\n },\n {\n "itemid": "1259108",\n "clock": "1664331488",\n "value": "14893608",\n "ns": "568884746"\n },\n {\n "itemid": "1259108",\n "clock": "1664331668",\n "value": "21814544",\n "ns": "42233867"\n },\n {\n "itemid": "1259108",\n "clock": "1664331848",\n "value": "13042016",\n "ns": "67987157"\n },\n {\n "itemid": "1259108",\n "clock": "1664332028",\n "value": "14084040",\n "ns": "333416432"\n },\n {\n "itemid": "1259108",\n "clock": "1664332208",\n "value": "14819768",\n "ns": "378817986"\n },\n {\n "itemid": "1259108",\n "clock": "1664332388",\n "value": "13573512",\n "ns": "224310824"\n },\n {\n "itemid": "1259108",\n "clock": "1664332568",\n "value": "15458744",\n "ns": "6257730"\n },\n {\n "itemid": "1259108",\n "clock": "1664332748",\n "value": "14190888",\n "ns": "131253545"\n },\n {\n "itemid": "1259108",\n "clock": "1664332928",\n "value": "14338440",\n "ns": "11481039"\n },\n {\n "itemid": "1259108",\n "clock": "1664333108",\n "value": "13999136",\n "ns": "368920025"\n },\n {\n "itemid": "1259108",\n "clock": "1664333288",\n "value": "19455520",\n "ns": "73571199"\n },\n {\n "itemid": "1259108",\n "clock": "1664333468",\n "value": "15880376",\n "ns": "534884519"\n },\n {\n "itemid": "1259108",\n "clock": "1664333648",\n "value": "14613928",\n "ns": "157824098"\n },\n {\n "itemid": "1259108",\n "clock": "1664333828",\n "value": "13903840",\n "ns": "68121173"\n }\n ]\n }\n ]\n }\n]”
},
“name”: “Function”,
“type”: “n8n-nodes-base.function”,
“typeVersion”: 1,
“position”: [
1920,
680
],
“id”: “ae499f08-5eba-4b57-ab2b-c6ae093d5152”
}
],
“connections”: {}
}

Share the output returned by the last node

Information on your n8n setup

  • **n8n version:193.5
  • **Database you’re using (default: SQLite):postgres
  • **Running n8n with the execution process [own(default), main]:own
  • **Running n8n via [Docker, npm, n8n.cloud, desktop app]:docker

I want to sort on “value” and only return a 1 item, ie the 5th item. I determine the Item I want buy the following {{ (($json.data.length)*.05+2).toFixed(0) }}

1 Like

You’ll probably want to use a function or code node and run against all items, returning only an array with the single item you want.

1 Like

This is based on the new n8n code node:

1 Like

Thank you, that is a very simple solution. For interest this is how I did it in Integromat where I am moving this from.

{{get(sort(87.array; “desc”; “value”); 85.95thRecord)}}value

1 Like

I’m glad I use n8n and not Integromat :stuck_out_tongue:

1 Like

Actually for completeness we should include the pairedItem so subsequent nodes know where the data came from

const indexToReturn = ($input.all().length * 0.5).toFixed(0);
return [
  {
    json: $input.all()[indexToReturn].json,
    pairedItem: indexToReturn,
  }
];

1 Like

Thanks bit late but this worked…