How can I get a first element from a sorted list?

c# sorted list with duplicates
generic sorted list c#
c# sorted array
c# sorted list get by index
c# sortedlist get last item
for each sorted list c#
return sorted list c#
c# initialize sorted list

I used Collections.sort(playersList); to sort a List. So, I think playersList is sorted now. But how can I get the first element of the list? playersList[0] does not work.


Java has limited operator polymorphism. So you use the get() method on List objects, not the array index operator ([])

How can I get a first element from a sorted list?, playersList.get(0). Java has limited operator polymorphism. So you use the get() method on List objects, not the array index operator ( [] ). A SortedList object internally maintains two arrays to store the elements of the list; that is, one array for the keys and another array for the associated values. Each element is a key/value pair that can be accessed as a DictionaryEntry object. A key cannot be null, but a value can be.

You have to access lists a little differently than arrays in Java. See the javadocs for the List interface for more information.


However if you want to find the smallest element in playersList, you shouldn't sort it and then get the first element. This runs very slowly compared to just searching once through the list to find the smallest element.

For example:

int smallestIndex = 0;
for (int i = 1; i < playersList.size(); i++) {
    if (playersList.get(i) < playersList.get(smallestIndex))
        smallestIndex = i;


The above code will find the smallest element in O(n) instead of O(n log n) time.

Find first and last positions of an element in a sorted array , C++ program to find first and last occurrence of. // an elements in given sorted array. #include<bits/stdc++.h>. using namespace std;. // Function for finding first  The built-in function, sorted, returns a new list, which is something you didn't seem to want to do. It returns a new list, which if you no longer need the old list would waste space in memory. Python automatically sorts on the first element. It then automatically sorts on the second, third, and so on.

That depends on what type your list is, for ArrayList use:


for LinkedList use:


if you like the array approach:


First strictly greater element in a sorted array in Java, Given a sorted array and a target value, find the first element that is strictly greater than given element. Examples: Input : arr[] = {1, 2, 3, 5, 8, 12} Target = 5 Output :  The SortedList class represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index. A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index.

Using Java 8 streams, you can turn your list into a stream and get the first item in a list using the .findFirst() method.

List<String> stringsList = Arrays.asList("zordon", "alpha", "tommy");
Optional<String> optional =;
optional.get(); // "zordon"

The .findFirst() method will return an Optional that may or may not contain a string value (it may not contain a value if the stringsList is empty).

Then to unwrap the item from the Optional use the .get() method.

C# SortedList, SortedList class implements IDictionary & ICollection interfaces, so elements Internally, SortedList maintains two object[] array, one for keys and another for  I would like to use Linq to query a bus schedule in my project, so that at any time I can get the next 5 bus arrival times. How can I limit my query to the first 5 results? More generally, how can I take a slice of a list in C#? (In Python I would use mylist[:5] to get the first 5 elements.)

Matthew's answer is correct:


To do what you tried:


you'll have to wait until Java 7 is released:

devoxx conference

Here's an interesting presentation by Mark Reinhold about Java 7

It looks like parleys site is currently down, try later :(

Find First and Last Position of Element in Sorted Array, Find First and Last Position of Element in Sorted Array. Medium. 2670 119. Add to List Share. Given an array of integers nums sorted in ascending order, find the​  sorted() Parameters. sorted() can take a maximum of three parameters: iterable - A sequence (string, tuple, list) or collection (set, dictionary, frozen set) or any other iterator. reverse (Optional) - If True, the sorted list is reversed (or sorted in descending order).

SortedList.GetByIndex(Int32) Method (System.Collections , Gets the value at the specified index of a SortedList object. Examples. The following code example shows how to get one or all the keys or values in a SortedList object. When an element is removed, the indexing also adjusts accordingly. Python List sort() The sort() method sorts the elements of a given list. The sort() method sorts the elements of a given list in a specific order - Ascending or Descending.

How to get first and last elements form ArrayList in Java, One of the common scenarios where you need first and last element of a list is supposed you have a sorted list and wants to get the highest and lowest element​? If it is actually a list of sets, then there is no first element because sets have no order. Here I’ve created a flat list because generally that seems more useful than creating a list of 1 element tuples. However, you can easily create a list of 1 element tuples by just replacing seq[0] with (seq[0],).

Insertion sort, The partial sorted list (black) initially contains only the first element in the list. With each iteration one element (  And we want to sort them in ascending order. All we do is call sort on the list, for in-place sorting, or the built in function sorted for not modifying the original list and returning a new sorted list. Both functions take in the same arguments and can be treated as "the same" for our purposes here, except for the reason above.

  • I wander why such a basic operation should be programed. Why Java does not provide a function which returns just a minimal value from array?
  • Roman, there is such a method, Collections.min (…).
  • bad advice. LinkedList implements the List interface, no need to use a special method (and I'd be amazed of there was any performance difference between the two). And calling toArray() is wasteful--you might be allocating the list into a new array for no reason!
  • @Kip, strange remark; the LinkedList class does not implement the first and last methods out of spite. If you have a good reason to use a LinkedList, you should not refrain from using its methods just because they are not in the List interface. The array example can be usefull if the list itself is not needed after sorting, and closest to what OP asked. Without knowing the context of the source code in question you cannot determine the validity of the advice.
  • why would it matter whether or not you need the list after sorting? in either case (or even if the list is never sorted) calling toArray() just to get the first element (may) needlessly create an entire array.
  • @Kip, you are right in that retrieving an array just for the first element is ot a good idea if you don not plan to use the array. The question asked for how to get the first element from a list and mentioned an array context. I merely gave alternatives touching those concepts. It is up to the programmer who knows his source to make a choice from these alternatives.
  • This doesn't seem to have actually made it into java 7