TECH need to sort json data via jquery

TS
TS
biawokauns

biawokauns

Active Member
Sep 18, 2001
19,873
Republic of Kalifornia
if anybody cares

Code:
// {'frequent':[
// 				{'departingCity':'Boston', 
// 				'arrivingCity':'Los Angeles',
// 				'departingCityCode':'BOS',
// 				'arrivingCityCode':'LAX',
// 				'departingDate':'08/02/2008',
// 				'arriveDate':'08/15/2008'},
// 
// 				{'departingCity':'X--Seattle', 
// 				'arrivingCity':'X--Los Angeles',
// 				'departingCityCode':'SEA',
// 				'arrivingCityCode':'LAX',
// 				'departingDate':'06/02/2008',
// 				'arriveDate':'06/15/2008'},
// 
// 
// 				{'departingCity':'Seattle', 
// 				'arrivingCity':'Los Angeles',
// 				'departingCityCode':'SEA',
// 				'arrivingCityCode':'LAX',
// 				'departingDate':'01/19/2008',
// 				'arriveDate':'01/25/2008'}
// 				]
// }
// 


// good to go?
$(document).ready(function(){

	//
	// // original code
	//
	// departureTmp = Date.fromString(item.departingDate);
	// departingDate = departureTmp.getDayName(true)+', '+departureTmp.getMonthName(true)+' '+item.departingDate.substring(3,5);
	// 
	// arriveTmp = Date.fromString(item.arriveDate);
	// arriveDate = arriveTmp.getDayName(true)+', '+arriveTmp.getMonthName(true)+' '+item.departingDate.substring(3,5)+' '+item.departingDate.substring(6,10);
	// 
	// 
	// $("ul#search-history").append('<li><a href="#">'+item.departingCity+' - '+item.arrivingCity+'</a><br/>'
	// +item.departingCityCode+' - '+item.arrivingCityCode+'<br />'
	// +departingDate+' - '+arriveDate+'<br/>'
	// +'</li>');
	

	// get JSON object
	$.getJSON("./ajax/trips.js", function(json){		
		
		// history
		var searchHistory = new Array();	
		$.each(json.history,function(i,item) {

			// build the array so it can be sorted
			// convert date to sortable #
			parsedDate = Date.parse(item.dateSearched);
			
			searchHistory[i] =	new Array(parsedDate,
				new Array(item.departingCity),
				new Array(item.arrivingCity), 
				new Array(item.arrivingCityCode),
				new Array(item.departingCityCode),								
				new Array(item.departingDate),							
				new Array(item.arriveDate)								
				);
		});	

		searchHistory = searchHistory.sort();
		
		// generate LIs	
		for (count=0; count<3; count++)
		{
			var departingCity		= searchHistory[count][1];
			var arrivingCity		= searchHistory[count][2];
			var arrivingCityCode	= searchHistory[count][3];		
			var departingCityCode	= searchHistory[count][4];		
			var departingDate		= searchHistory[count][5][0];		
			var arriveDate			= searchHistory[count][6][0];		
			

			departureTmp = Date.fromString(departingDate);			
			departingDate = departureTmp.getDayName(true)+', '+departureTmp.getMonthName(true)+' '+departingDate.substring(3,5);

			arriveTmp = Date.fromString(arriveDate);
			arriveDate = arriveTmp.getDayName(true)+', '+arriveTmp.getMonthName(true)+' '+arriveDate.substring(3,5)+' '+searchHistory[count][5][0].substring(6,10);	
		
			$("ul#search-history").append('<li><a href="#">'+departingCity+' - '+arrivingCity+'</a><br/>'
			+departingCityCode+' - '+arrivingCityCode+'<br />'
			+departingDate+' - '+arriveDate+'<br/>'
			+'</li>');
			
		}

 


		// upcoming trips
		var upcomingTrips = new Array();	
		$.each(json.upcoming,function(i,item) {

			// build the array so it can be sorted
			
			// convert date to sortable #
			parsedDate = Date.parse(item.departingDate);
			
			upcomingTrips[i] =	new Array(parsedDate,
				new Array(item.departingCity),
				new Array(item.arrivingCity), 
				new Array(item.arrivingCityCode),
				new Array(item.departingCityCode),								
				new Array(item.departingDate),							
				new Array(item.arriveDate)								
				);
		});	
		
		// sort, then invert
		upcomingTrips = upcomingTrips.sort();
		upcomingTrips = upcomingTrips.reverse();
		
		// generate LIs	
		for (count=0; count<3; count++)
		{
			var departingCity		= upcomingTrips[count][1];
			var arrivingCity		= upcomingTrips[count][2];
			var arrivingCityCode	= upcomingTrips[count][3];		
			var departingCityCode	= upcomingTrips[count][4];		
			var departingDate		= upcomingTrips[count][5][0];		
			var arriveDate			= upcomingTrips[count][6][0];		
			

			departureTmp = Date.fromString(departingDate);			
			departingDate = departureTmp.getDayName(true)+', '+departureTmp.getMonthName(true)+' '+departingDate.substring(3,5);

			arriveTmp = Date.fromString(arriveDate);
			arriveDate = arriveTmp.getDayName(true)+', '+arriveTmp.getMonthName(true)+' '+arriveDate.substring(3,5)+' '+upcomingTrips[count][5][0].substring(6,10);	
			
			$("ul#upcoming-trips").append('<li><a href="#">'+departingCity+' - '+arrivingCity+'</a><br/>'
			+departingCityCode+' - '+arrivingCityCode+'<br />'
			+departingDate+' - '+arriveDate+'<br/>'
			+'</li>');
		}



		
		// frequent trips
		var frequentCities = new Array();	
		$.each(json.frequent,function(i,item) {

			// build the array so it can be sorted
			frequentCities[i] =	new Array(item.departingCity+i,
				new Array(item.departingCity),
				new Array(item.arrivingCity), 
				new Array(item.arrivingCityCode),
				new Array(item.departingCityCode),								
				new Array(item.departingDate),							
				new Array(item.arriveDate)								
				);
		});	

		frequentCities.sort();
		
		// generate LIs	
		for (count=0; count<3; count++)
		{
			var departingCity		= frequentCities[count][1];
			var arrivingCity		= frequentCities[count][2];
			var arrivingCityCode	= frequentCities[count][3];		
			var departingCityCode	= frequentCities[count][4];		
			var departingDate		= frequentCities[count][5][0];		
			var arriveDate			= frequentCities[count][6][0];	
			
			departureTmp = Date.fromString(departingDate);			
			departingDate = departureTmp.getDayName(true)+', '+departureTmp.getMonthName(true)+' '+departingDate.substring(3,5);

			arriveTmp = Date.fromString(arriveDate);
			arriveDate = arriveTmp.getDayName(true)+', '+arriveTmp.getMonthName(true)+' '+arriveDate.substring(3,5)+' '+frequentCities[count][5][0].substring(6,10);
			
			
			$("ul#frequent-trips").append('<li><a href="#">'+departingCity+' - '+arrivingCity+'</a><br/>'
			+departingCityCode+' - '+arrivingCityCode+'<br />'
			+departingDate+' - '+arriveDate+'<br/>'
			+'</li>');
		}

		// when jquery inserts the above LIs, the rounded corners don't appear on the footer. let's insert them
		// after we've create the lists
		$("#saved-searches-dropdown span.tr").after('<span class="br corner"></span><span class="bl corner"></span>');
	
	});
	

	// saved search link functionality 
    $("div#saved-searches a").click(function () {
      if ($("div#saved-searches-dropdown").is(":hidden")) {
        $("div#saved-searches-dropdown").fadeIn("fast");
      } else {
        $("div#saved-searches-dropdown").fadeOut("fast");
      }
    });

});
 

Users who are viewing this thread

About Us

  • Please do not post anything that violates any Local, State, Federal or International Laws. Your privacy is protected. You have the right to be forgotten. Site funded by advertising, link monetization and member support.
OT v15.8.1 Copyright © 2000-2022 Offtopic.com
Served by fu.offtopic.com

Online statistics

Members online
426
Guests online
54
Total visitors
480

Forum statistics

Threads
369,464
Messages
16,885,758
Members
86,873
Latest member
vitalesan