У меня есть эта база данных и эта функция сортировки.Сортировка выполняется в HTML-таблицу, которая взяла данные из файла database.json.Проблема в том, что когда он сортирует, он сортирует их по индексу, а не по имени местоположения, как было задано.Например, в этом случае он только обратит первый набор данных с последним.(1,2,3) -> (3,2,1)
Пожалуйста, помогите ...
БАЗА ДАННЫХ
[
{
"title": "Great big event",
"location": "Grand plaza"
},
{
"title": "Cross cultural food tasting",
"location": "Melius Restaurant",
"date": "18 December 2018",
"time": "8 PM",
"description" : "Food tasting event where you can enjoy a mix of 10 famous cuisines"
},
{
"title": "Small dining event",
"location": "Cafe Il diroma",
"date": "10 February 2019",
"time": "6 AM",
"description": "The perfect place for casual dinners"
}
]
ФУНКЦИЯ СОРТИРОВКИ
location.sort(function(a, b){
if(a.location < b.location) { return -1; }
if(a.location > b.location) { return 1; }
return 0;
})
</script>
<body>
<div class="show-inline-block">
<button onclick="location.sort(a,b)" class="button button2">Sort table</button>
</div>
<div id="result"></div>
</body>
ОБНОВЛЕНИЕ: Сортировка выполняется в таблицу HTML, созданную с данными из database.json
<div class="table-responsive">
<table class="table table-striped" id="myTable" >
<thead>
<tr>
% for k in displaykeys:
<th scope="col" style="padding-left:30px">{{k.title()}}</th>
% end # Usual python indention to structure code does not work in .tpl files - "end" is used instead to end a block
</tr>
</thead>
<tbody>
% for i, d in enumerate(displaydata): # displaydata is expected to be a list of dictionaries
% link_url = "/events/" + str(i + 1) # relative url to detailed view
<tr>
% for k in displaykeys: # Go thru the keys in the same order as for the headline row
<td style="padding-left:30px"><a href="{{link_url}}" alt="See details">{{displaydata[i][k]}}</a></td>
% end # Usual python indention to structure code does not work in .tpl files - "end" is used instead to end a block
</tr>
% end
</tbody>
</table>
</div>