Thursday, October 6, 2016

Alphanumeric Sorting in JavaScript using sort function


Sorting using sort() function in JavaScript


Function sort() is basically used for sorting an array in JavaScript.

Let us take few sample codes and see how this function behaves with arrays of different types of elements such as numbers , strings , alphanumeric etc.

Example 1: 
var ar = [11,23,7,189,9,550];
var result = ar.sort();
console.log(result);

Output: [ 11, 189, 23, 550, 7, 9 ]
  • Conclusion : The above example sorts the array of Numbers is lexical / dictionary order.

Example 2: 
var ar = [11,23,7,189,9,550];
var result = ar.sort(numberic_sort);
console.log(result);

//This function cannot be used for string comaprision
function numberic_sort(a,b){
return a-b;
};

Output : [ 7, 9, 11, 23, 189, 550 ]
  • Conclusion : The above customized function sorts the array of numbers in ascending order.

Example 3:
var ar = ["as","asddd","trhs","oocww","wfewf","kcnw","qmdkvcn","acwwev"];
var result = ar.sort();
console.log(result);

Output: 
'acwwev',
'as',
'asddd',
'kcnw',
'oocww',
'qmdkvcn',
'trhs',
'wfewf' ]
  • Conclusion : The above example sorts the array of Strings is lexical / dictionary order.

Example 4:
var ar = ["csnc33","cddsc1","cwew91","fdwef62","vc2211","2831nb1","7dsv7sv","ncnd777","22nbebe","ewfwef873bttbbbbbgc","fdwef69","fdwef61","123","njahy","125","0.342"];
var result = ar.sort();
console.log(result);

Output : [
'0.342',
'123',
'125',
'22nbebe',
'2831nb1',
'7dsv7sv',
'cddsc1',
'csnc33',
'cwew91',
'ewfwef873bttbbbbbgc',
'fdwef61',
'fdwef62',
'fdwef69',
'ncnd777',
'njahy',
'vc2211' ]
  • Conclusion : The above example also sorts the array of Strings is lexical / dictionary order.

Example 5:
var ar = ["A","a","1",1,"cascsac","cccead","treave","mvwen","VV344FFVveve323","v8e8v98vvd","3234223","3332csdvv3","323","3451","68564","11243","562",45,2352,255,0.432,64.223,"5545.454"];
var result = ar.sort();
console.log(result);

Output : 
0.432,
1,
'1',
'11243',
2352,
255,
'323',
'3234223',
'3332csdvv3',
'3451',
45,
'5545.454',
'562',
64.223,
'68564',
'A',
'VV344FFVveve323',
'a',
'cascsac',
'cccead',
'mvwen',
'treave',
'v8e8v98vvd' ]

Conclusion: 
  • By default ,All the values are compared as strings in sort function , hence sorting is in lexical/dictionary order.
  • Sorting Order :  numbers -> capital Alphabets -> small alphabets
  • Example  :  [1, "1", "1a", "A" ,"a", "b1"]  (Ascending order) 

Let us take Some more example , but now with Comparison Operators such as  > , <  , == etc


  • 1 < 2 true
  • "1" < "2"  true
  • "1" == 1  true
  • "1" < "a"   true
  • "1" < "sacacasc"  true
  • "0.3" < "svs"  true
  • "sscsd" < "sscsd8776"  true

As you can see from above examples that comparison operator also woks similar as sort function.
It also compares values as strings and hence gives results according to lexical order. 

Uninstalling MongoDB from ubuntu


Uninstalling MongoDB from Ubuntu Step-Wise


Open Terminal in ubuntu using Ctrl+Alt+T.
Run the below commands :

  • sudo service mongod stop
  • sudo apt-get purge mongodb-org*
  • sudo rm -r /var/log/mongodb
  • sudo rm -r /var/lib/mongodb


The above commands will remove the MongoDB installation from ubuntu.

For Reinstalling MongoDB Please refer : Installing MongoDB in Ubuntu.




Installing MongoDB in Ubuntu


Step-wise Installation of MongoDB in Ubuntu 


Note: We will be installing community edition of MongoDB 

Open Terminal in ubuntu using Ctrl+Alt+T.
Run the below commands :

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927


# For Ubuntu 12.04
  • echo "deb http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

# For Ubuntu 14.04
  • echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
# For Ubuntu 16.04
  • echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

sudo apt-get update

# To install latest stable version of MongoDB
sudo apt-get install -y mongodb-org


To install a specific release, you must specify each component package individually along with the version number, as in the following example:

sudo apt-get install -y mongodb-org=3.2.10 mongodb-org-server=3.2.10 mongodb-org-shell=3.2.10 mongodb-org-mongos=3.2.10 mongodb-org-tools=3.2.10

Follow the below step only for Ubuntu 16.04

Create a new file at /lib/systemd/system/mongod.service with the following contents:

[Unit] 

Description=High-performance, schema-free document-oriented database After=network.target
Documentation=https://docs.mongodb.org/manual

[Service] 

User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]



WantedBy=multi-user.target

Commands for Starting, Stopping, restarting MongoDB


  • sudo service mongod start
  • sudo service mongod stop
  • sudo service mongod restart


To verify that MongoDB is running , check the logs file at below path

/var/log/mongodb/mongod.log


This log file must have
[initandlisten] waiting for connections on port <port>

If the log file have above line , MongoDB is up and running.

For Uninstalling  MongoDB , please refer : UnInstalling MongoDB in Ubuntu



Installing MongoDB in Windows


Step-wise Installation on MongoDB Current Stable Release (3.2.10)


Download MongoDB from the below Url 


The trick here is to download the appropriate version of mongo. There are multiple types of mongo servers but we will use either of Community Server or Enterprise Server.
Community servers are used for development and testing purposes while enterprise edition is used for commercial purposes .
Also there are choices for 62bit/32bit . But all these installations are best supported in windows 7 and above.


Install the downloaded file with all default options.

Now, Create a directory anywhere which will serve as a root directory for our database.
e.g mkdir -p C:\mongo\data\db

Set the MongoDB installation path in environment Variable , if it is not already set.

By default installation directory is
C:\Program Files\MongoDB\Server\3.0\bin

Set above path in environment variable if you have used default installation directory.

Now we are ready to start MongoDB.
To start MongoDB  , Run the below command in command prompt:

mongod --dbpath c:\mongo\data\db

With --dbpath provide the path root directory created for sorting database.

If you see below screen without any prompt coming again, then no error has occurred and  MongoDB server is running Successfully.

To verify that mongo server is running ,Leave this command prompt running.Open new        command prompt and run below command.

mongo localhost:27017

This command will try to connect  to MongoDB server running on localhost on port 27017 (By default). If you have mongo running on some different IP and port (custom configuration), you can use those ip:port also.




Now if you see mongo shell running as shown in below screenshot , than MongoDB is successfully installed and running.