-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.js
148 lines (132 loc) · 3.47 KB
/
sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
function sortFunction(movieArray, sortType)
{
if (sortType != "None")
{
let sortID = getSortID(sortType);
let sortMeth = $("#"+ sortID +"").children("option:selected").val();
// SORT TYPES
if (sortType == "genre_ids")
movieArray = sortGenre(movieArray, sortMeth, "sort"); // sortMeth is the genre.id in this case
// SORT METHODS
if (sortMeth == "asc")
movieArray = sortAscending(movieArray, sortType);
else if (sortMeth == "desc")
movieArray = sortDescending(movieArray, sortType);
// this is used to remove or append movies that don't have ratings
if (sortType == "imdbRating")
movieArray = appendNoRating(movieArray, sortMeth); //movieArray = removeNoRating(movieArray);
}
return movieArray;
}
function sortAscending(result, field)
{
let arr = [];
for(let i = 0; i < result.length; i++)
{
for(let j = i; j < result.length; j++)
{
if (result[i][field] > result[j][field])
[result[i], result[j]] = [result[j], result[i]]; // simplified swap
}
arr.push(result[i]);
}
return arr;
}
function sortDescending(result, field)
{
let arr = [];
for(let i = 0; i < result.length; i++)
{
for(let j = i; j < result.length; j++)
{
if (result[i][field] < result[j][field])
[result[i], result[j]] = [result[j], result[i]]; // simplified swap
}
arr.push(result[i]);
}
return arr;
}
// look for selected genre and put that at the top.
// in the case that we want only the selected genre to show, remove the concat
function sortGenre(result, genreID, type)
{
let arr = [];
let arr2 = [];
// iterate through the movie arrays
for(let i = 0; i < result.length; i++)
{
// iterate through the genre array
for(let j = 0; j < result[i].genre_ids.length; j++)
{
if (result[i].genre_ids[j] == genreID)
{
if (!(arr.includes(result[i])))
{
arr.push(result[i]);
console.log(arr);
}
}
}
if ((!(arr.includes(result[i]))) && (!(arr.includes(result[i]))))
arr2.push(result[i]);
}
if (type == "sort")
arr = arr.concat(arr2);
return arr;
}
//Rating
// remove results that have no rating
function removeNoRating(result)
{
let arr = [];
for(let i = 0; i < result.length; i++)
{
if (!(result[i].imdbRating === 'N/A' || result[i].imdbRating === 'undefined' || result[i].imdbRating === undefined || result[i].imdbRating === 'null' && result[i].imdbRating === null || isNaN(result[i].imdbRating)))
{
arr.push(result[i]);
}
}
return arr;
}
// place not rating items at the end
function appendNoRating(result, sort)
{
let arr = [];
let arr2 = [];
for(let i = 0; i < result.length; i++)
{
if (!(result[i].imdbRating === 'N/A' || result[i].imdbRating === 'undefined' || result[i].imdbRating === undefined || result[i].imdbRating === 'null' && result[i].imdbRating === null || isNaN(result[i].imdbRating)))
{
arr.push(result[i]);
}
else
arr2.push(result[i]);
}
if (sort == "asc")
arr = arr2.concat(arr);
else if (sort == "desc")
arr = arr.concat(arr2);
return arr;
}
// removes any duplicate movies just in case
function remove_Dup(arr)
{
var result = []; // this is what must be returned
var temp = []; // this array will store the ID and can compare against
for (var i = 0; i < arr.length; i++)
{
for (var j = 1; j < arr.length; j++)
{
if (arr[i].imdbID != arr[j].imdbID)
{
if (!(temp.includes(arr[i].imdbID)))
{
temp.push(arr[i].imdbID);
if (!(result.includes(arr[i])))
result.push(arr[i]);
}
}
}
}
return result;
}