Sql While Loop Through Array Of Objects Sort In Javascript