JavaScript basit dizi ve JSON obje dizisi,nesne sıralama,sınıflandırma kodu kullanımı örneği (JavaScript simple array and JSON Object array sorting,classificaiton code usage example)

JavaScript’te basit dizi nesnesi ve JSON obje dizi nesnesinin içeriğini sıralayıp görmek için “sort” fonksiyonunu kullanarak basit dizi nesnesi ve JSON dizi nesnesinin içeriğini tek tek karşılaştırarak aşağıdaki şekilde sıralayabilirsiniz.

<!DOCTYPE html>
<html>
<head>
	<title>sort json object array and array | yazilimcity.net</title>
	<meta charset="UTF-8">

	<script type="text/javascript" language="javascript">
		var simpleDataArrayObj = ["a", "g", "d","c","f","e","b"];

		function unsortedSimpleDataArrayObjShow() {
			var unsortedSimpleDataArrayObj = document.getElementById("unsortedSimpleDataArrayObjP");
			unsortedSimpleDataArrayObj.innerHTML = simpleDataArrayObj;

			//hide show unsorted button
			document.getElementById("showUnsortedSimpleDataArrayObjBtn").style.visibility= 'hidden';
		}

		function sortSimpleDataArrayObj() {
			//sort
			var tmpSimpleDataArray = simpleDataArrayObj;
			var sortedSimpleDataArrayObj = tmpSimpleDataArray.sort();

			var sortedSimpleDataArrayObjP = document.getElementById("sortedSimpleDataArrayObjP");
			sortedSimpleDataArrayObjP.innerHTML = sortedSimpleDataArrayObj;
		}
	</script>

	<script type="text/javascript" language="javascript">
		var jsonObjectArray = [
			{name: "serap", age: "28"},
			{name: "oguz", age: "28"},
			{name: "eda", age: "24"},
			{name: "münevver", age: "25"},
			{name: "ethem", age: "31"},
			{name: "murat", age: "22"}
		];

		function showUnsortedJsonObjArray() {

			var str = "";
			for(var i=0; i<jsonObjectArray.length; i++) {
				str += jsonObjectArray[i].name + "-" + jsonObjectArray[i].age + "\n";
			}

			var unsortedJsonObjArrayP = document.getElementById("unsortedJsonObjArrayP");
			unsortedJsonObjArrayP.innerHTML=str;


			//hide show sorted button
			document.getElementById("showUnsortedJsonObjArrayBtn").style.visibility= 'hidden';
		}

		function compare(a,b) {
			if (a.name < b.name)
				return -1;
			if (a.name > b.name)
				return 1;
			return 0;
		}

		function compareAge(a,b) {
			if (a.age < b.age)
				return -1;
			if (a.age > b.age)
				return 1;
			return 0;
		}

		function sortJsonObjArray() {
			var tmpJsonObjectArray = jsonObjectArray;

			//first sort by name
			tmpJsonObjectArray.sort(compare);
			//first sort by age
			tmpJsonObjectArray.sort(compareAge);

			var str = "";
			for(var i=0; i<tmpJsonObjectArray.length; i++) {
				str += tmpJsonObjectArray[i].name + "-" + tmpJsonObjectArray[i].age + "\n";
			}

			var sortedJsonObjArrayP = document.getElementById("sortedJsonObjArrayP");
			sortedJsonObjArrayP.innerHTML=str;

		}

	</script>
</head>
<body>

	<button id="showUnsortedSimpleDataArrayObjBtn" onclick="unsortedSimpleDataArrayObjShow();">Show UnsortedSimpleDataArrayObj</button>
	<button onclick="sortSimpleDataArrayObj();">Show sortedSimpleObj</button>

	<p id="unsortedSimpleDataArrayObjP">unsortedSimpleDataArrayObj</p>
	<p id="sortedSimpleDataArrayObjP">sortedSimpleDataArrayObj</p>

	<button id="showUnsortedJsonObjArrayBtn" onclick="showUnsortedJsonObjArray();">Show UnsortedJsonObjArray</button>
	<button onclick="sortJsonObjArray();">Show sortedJsonObjArray</button>

	<p id="unsortedJsonObjArrayP">unsortedJsonObjArray</p>
	<p id="sortedJsonObjArrayP">sortedJsonObjArray</p>

	<footer>
		<a href="http://www.yazilimcity.net">
			<address>www.yazilimcity.net</address>
		</a>
	</footer>
</body>
</html>
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on StumbleUpon0Share on Reddit0

Leave a Reply


*