Last active
February 23, 2024 15:52
-
-
Save Aviortheking/50891c738e9fe278467925bc2c6f97ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Newtonsoft.Json.Linq; | |
/* | |
Énoncé: | |
Créez une fonction de tri qui prend en entrée un tableau d'objets et une clé de tri. | |
La fonction doit trier le tableau en fonction de la clé spécifiée de manière ascendante ou descendante. | |
Exemple d'utilisation pour le JSON suivant: | |
[ | |
{ "nom": "John", "age": 30 }, | |
{ "nom": "Alice", "age": 25 }, | |
{ "nom": "Bob", "age": 35 } | |
] | |
SortData(personnes, 'age', 'asc'); | |
// Résultat attendu: [{ nom: 'Alice', age: 25 }, { nom: 'John', age: 30 }, { nom: 'Bob', age: 35 }] | |
SortData(personnes, 'age', 'desc'); | |
// Résultat attendu: [{ nom: 'Bob', age: 35 }, { nom: 'John', age: 30 }, { nom: 'Alice', age: 25 }] | |
*/ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string inputJson = System.IO.File.ReadAllText("input.json"); | |
JObject json = JObject.Parse(inputJson); | |
// Example usage | |
var cards = json["data"]["cards"].ToObject<List<Card>>(); | |
var sortedByHP = SortData(cards, "hp", "asc"); | |
Console.WriteLine("Sorted by HP:"); | |
foreach (var card in sortedByHP) | |
{ | |
Console.WriteLine(card); | |
} | |
var sortedByName = SortData(cards, "name", "desc"); | |
Console.WriteLine("Sorted by Name:"); | |
foreach (var card in sortedByName) | |
{ | |
Console.WriteLine(card); | |
} | |
} | |
static List<T> SortData<T>(List<T> input, string key, string order) where T : IComparable | |
{ | |
// run you code here | |
} | |
class Card | |
{ | |
public string id { get; set; } | |
public string name { get; set; } | |
public int hp { get; set; } | |
public override string ToString() | |
{ | |
return $"ID: {id}, Name: {name}, HP: {hp}"; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": { | |
"cards": [ | |
{ | |
"id": "sma-SV1", | |
"name": "Scyther", | |
"set": { | |
"id": "sma", | |
"name": "Yellow A Alternate" | |
}, | |
"hp": 70 | |
}, | |
{ | |
"id": "sma-SV10", | |
"name": "Quagsire", | |
"set": { | |
"id": "sma", | |
"name": "Yellow A Alternate" | |
}, | |
"hp": 120 | |
}, | |
{ | |
"id": "swsh4.5-SV100", | |
"name": "Greedent", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 120 | |
}, | |
{ | |
"id": "swsh4.5-SV101", | |
"name": "Rookidee", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 60 | |
}, | |
{ | |
"id": "swsh4.5-SV102", | |
"name": "Corvisquire", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 80 | |
}, | |
{ | |
"id": "swsh4.5-SV103", | |
"name": "Wooloo", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 70 | |
}, | |
{ | |
"id": "swsh4.5-SV104", | |
"name": "Dubwool", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 130 | |
}, | |
{ | |
"id": "swsh4.5-SV105", | |
"name": "Rillaboom V", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 220 | |
}, | |
{ | |
"id": "swsh4.5-SV106", | |
"name": "Rillaboom VMAX", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 330 | |
}, | |
{ | |
"id": "swsh4.5-SV107", | |
"name": "Charizard VMAX", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 330 | |
}, | |
{ | |
"id": "swsh4.5-SV108", | |
"name": "Centiskorch V", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 210 | |
}, | |
{ | |
"id": "swsh4.5-SV109", | |
"name": "Centiskorch VMAX", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 320 | |
}, | |
{ | |
"id": "sma-SV11", | |
"name": "Froakie", | |
"set": { | |
"id": "sma", | |
"name": "Yellow A Alternate" | |
}, | |
"hp": 50 | |
}, | |
{ | |
"id": "swsh4.5-SV110", | |
"name": "Lapras V", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 210 | |
}, | |
{ | |
"id": "swsh4.5-SV111", | |
"name": "Lapras VMAX", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 320 | |
}, | |
{ | |
"id": "swsh4.5-SV112", | |
"name": "Toxtricity V", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 210 | |
}, | |
{ | |
"id": "swsh4.5-SV113", | |
"name": "Toxtricity VMAX", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 320 | |
}, | |
{ | |
"id": "swsh4.5-SV114", | |
"name": "Indeedee V", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 180 | |
}, | |
{ | |
"id": "swsh4.5-SV115", | |
"name": "Falinks V", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 160 | |
}, | |
{ | |
"id": "swsh4.5-SV116", | |
"name": "Grimmsnarl V", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 220 | |
}, | |
{ | |
"id": "swsh4.5-SV117", | |
"name": "Grimmsnarl VMAX", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 330 | |
}, | |
{ | |
"id": "swsh4.5-SV118", | |
"name": "Ditto V", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 170 | |
}, | |
{ | |
"id": "swsh4.5-SV119", | |
"name": "Ditto VMAX", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 320 | |
}, | |
{ | |
"id": "sma-SV12", | |
"name": "Frogadier", | |
"set": { | |
"id": "sma", | |
"name": "Yellow A Alternate" | |
}, | |
"hp": 80 | |
}, | |
{ | |
"id": "swsh4.5-SV120", | |
"name": "Dubwool V", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 210 | |
}, | |
{ | |
"id": "swsh4.5-SV121", | |
"name": "Eternatus V", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 220 | |
}, | |
{ | |
"id": "swsh4.5-SV122", | |
"name": "Eternatus VMAX", | |
"set": { | |
"id": "swsh4.5", | |
"name": "Shining Fates" | |
}, | |
"hp": 340 | |
}, | |
{ | |
"id": "sma-SV13", | |
"name": "Voltorb", | |
"set": { | |
"id": "sma", | |
"name": "Yellow A Alternate" | |
}, | |
"hp": 50 | |
}, | |
{ | |
"id": "sma-SV14", | |
"name": "Xurkitree", | |
"set": { | |
"id": "sma", | |
"name": "Yellow A Alternate" | |
}, | |
"hp": 120 | |
}, | |
{ | |
"id": "sma-SV15", | |
"name": "Seviper", | |
"set": { | |
"id": "sma", | |
"name": "Yellow A Alternate" | |
}, | |
"hp": 100 | |
}, | |
{ | |
"id": "sma-SV16", | |
"name": "Shuppet", | |
"set": { | |
"id": "sma", | |
"name": "Yellow A Alternate" | |
}, | |
"hp": 60 | |
}, | |
{ | |
"id": "sma-SV17", | |
"name": "Inkay", | |
"set": { | |
"id": "sma", | |
"name": "Yellow A Alternate" | |
}, | |
"hp": 60 | |
}, | |
{ | |
"id": "sma-SV18", | |
"name": "Malamar", | |
"set": { | |
"id": "sma", | |
"name": "Yellow A Alternate" | |
}, | |
"hp": 90 | |
}, | |
{ | |
"id": "sma-SV19", | |
"name": "Poipole", | |
"set": { | |
"id": "sma", | |
"name": "Yellow A Alternate" | |
}, | |
"hp": 70 | |
} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
# Load data from input.json | |
with open('input.json') as f: | |
data = json.load(f) | |
# Example usage | |
cards = data['data']['cards'] | |
def sort_data(input, key, order): | |
# code | |
# Sort by HP in ascending order | |
sorted_by_hp = sort_data(cards, 'hp', 'asc') | |
print("Sorted by HP in ascending order:") | |
print(sorted_by_hp) | |
# Sort by Name in descending order | |
sorted_by_name = sort_data(cards, 'name', 'desc') | |
print("\nSorted by Name in descending order:") | |
print(sorted_by_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment