import networkx as nx def sortEdges(graph, direction='asc'): sorted_edges = sorted(graph.edges(data=True), key=lambda x: x[2]['weight'], reverse=(direction=='desc')) return [(u, v) for u, v, w in sorted_edges] # Create a graph with weighted edges G = nx.Graph() G.add_edge(1, 2, weight=2) G.add_edge(1, 3, weight=1) G.add_edge(2, 3, weight=3) # Sort the edges in ascending order sorted_edges = sortEdges(G) print(sorted_edges) # [(1, 3), (1, 2), (2, 3)] # Sort the edges in descending order sorted_edges = sortEdges(G, direction='desc') print(sorted_edges) # [(2, 3), (1, 2), (1, 3)]In this example, the `sortEdges` function is used to sort the edges of a graph `G` in ascending and descending order based on their weights. The `sorted` function is used with a `key` parameter to specify the weight attribute of each edge. The `reverse` parameter is used to specify the sort order. The `networkx` package library is used to create and manipulate graphs.