sorting

Sort dictionary in Python

Posted on

Python dictionary stores data in key-value pairs. In some situations, programmers need to sort the dictionary object as per their requirement which may be based on key or value. To sort dictionary object, you can user sorted() method and pass either keys or values from the dictionary which sorts the elements in their natural order. For example, if the elements are all integers, then smaller numbers go earlier in the list. If the elements are all strings, they are arranged in alphabetic order. Below examples demonstrate different ways to sort the dictionary based on key or value. Read the rest of this entry »

Difference between sorted() and list.sort() method

Posted on Updated on

There are different ways to sort a list in Python. You can find examples to sort a list in our article sort list in Python. Most popular ways are using sorted() and list.sort() methods. Each method has some advantage over other. In this article, we will be discussing difference between sorted() and list.sort() method. Read the rest of this entry »

Sort list in Python

Posted on Updated on

Python has different methods to sort elements of a list. These methods arrange elements in their natural order either in ascending or descending order. For example, if the elements are all integers, then smaller numbers go earlier in the list. If the elements are all strings, they are arranged in alphabetic order. Below are examples to sort list using list.sort(), sorted() method in ascending and descending order. Read the rest of this entry »

Sort a Map in Java Example

Posted on Updated on

A map is an data structure class that stores associations between keys and values which are widely used in java programming language. As map stores key-value pair, we can can sort map by two ways.

  1. Sort by keys
  2. Sort by values

Let us see example to sort map in both ways. Read the rest of this entry »