DevShorts3: Sort 2-D array based on first index

--

Convert arrayList to array in java:for 1 d array: 
//al is arrayList
Integer[] arr = new int[al.size()];
arr = al.toArray(arr);
for 2 d array:
Integer[][] arr = new int[al.size()][];
arr = al.toArray(arr);
Java:
Arrays.sort(intervals,
(arr1,arr2)->Integer.compare(arr1[0],arr2[0]));
or
Arrays.sort(intervals, (a1,a2)-> a1[0]- a2[0]);
Arrays.sort(intervals,
Collections.reverseOrder((arr1,arr2)->Integer.compare(arr1[0],arr2[0])));
Python:list_name.sort(key=lambda x:x[0],reverse=False)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Mohammad Farman Abbasi
Mohammad Farman Abbasi

Written by Mohammad Farman Abbasi

Learning each day without giving up.

No responses yet

Write a response