0

I am storing stock data in a JSON file that looks like this:

{
  "06/01/2021 18:41:02": {
    "NASDAQ-BYND": {
      "Sell": "6",
      "Neutral": "9",
      "Buy": "11"
    },
    "NYSE-CCL": {
      "Sell": "3",
      "Neutral": "8",
      "Buy": "15"
    },
    "NASDAQ-NVDA": {
      "Sell": "2",
      "Neutral": "8",
      "Buy": "16"
    },
    "NASDAQ-TQQQ": {
      "Sell": "2",
      "Neutral": "8",
      "Buy": "16"
    }
  }
}

I am trying to sort the dictionary by the 'Buy' value so that my result looks like this:

{
    "NASDAQ-NVDA": {
      "Sell": "2",
      "Neutral": "8",
      "Buy": "16"
    },
    "NASDAQ-TQQQ": {
      "Sell": "2",
      "Neutral": "8",
      "Buy": "16"
    },
    "NYSE-CCL": {
      "Sell": "3",
      "Neutral": "8",
      "Buy": "15"
    },
    "NASDAQ-BYND": {
      "Sell": "6",
      "Neutral": "9",
      "Buy": "11"
    }
}

My code currently looks like this:

with open("/dayData.json", 'r') as day:
    data = json.load(day)
    keys = list(data)

print(sorted(data[keys[len(keys)-1]].items(), key = lambda x: x[1]['Buy'])

The reason I store the initial keys in the keys variable is because the original JSON file is quite large and I only want to get the data from the last dated entry. Calling list(data) only returns the initial parent keys which are all of the dates. The result of running the code above:

{
  "NASDAQ-BYND": { "Sell": "6", "Neutral": "9", "Buy": "11" },
  "NYSE-CCL": { "Sell": "3", "Neutral": "8", "Buy": "15" },
  "NASDAQ-NVDA": { "Sell": "2", "Neutral": "8", "Buy": "16" },
  "NASDAQ-TQQQ": { "Sell": "2", "Neutral": "8", "Buy": "16" },
  "NYSE-AMC": { "Sell": "1", "Neutral": "9", "Buy": "16" },
  "NASDAQ-AMZN": { "Sell": "14", "Neutral": "10", "Buy": "2" },
  "NASDAQ-AAPL": { "Sell": "13", "Neutral": "10", "Buy": "3" },
  "NASDAQ-COIN": { "Sell": "7", "Neutral": "7", "Buy": "3" },
  "NASDAQ-MARA": { "Sell": "10", "Neutral": "9", "Buy": "7" },
  "NASDAQ-TSLA": { "Sell": "9", "Neutral": "8", "Buy": "9" }
}

What am I doing wrong or need to know?

G T
  • 31
  • 3
  • But `"11"` is correctly sorted before `"15"`, `"16"`, and `"2"`? – mkrieger1 Jun 02 '21 at 17:27
  • 3
    Does this answer your question? [Sorting a list of strings numerically](https://stackoverflow.com/questions/52737587/sorting-a-list-of-strings-numerically) – mkrieger1 Jun 02 '21 at 17:29
  • @mkrieger1 I made a poor choice of what data to show and I am aware that the data is already organized in an ascending order, but I am trying to sort the stock keys ("NASDAQ-APPLE, NASDAQ-TSLA") by their "Buy" values in descending order. The other question you linked does not solve my problem as it refers to sorting a list not a nested dictionary. – G T Jun 02 '21 at 17:33
  • I guess your question is why the result is sorted by the "Buy" values *alphabetically* and not *numerically*? – mkrieger1 Jun 02 '21 at 17:38
  • That you wanted to sort in descending order is new information. In that case also use `reverse=True` as argument to `sorted`, in addition to converting the "Buy" values to integers in the `key` function (alternatively, negate the resulting integers). – mkrieger1 Jun 02 '21 at 17:41
  • @mkrieger1 Yes, I suppose that is the root question of what I want to know. Is that what is occuring in my code? Are the values of 'Buy' keys being sorted alphabetically and not _numerically?_ Thanks. – G T Jun 02 '21 at 17:43
  • Yes they are, because they are strings. Convert them to integers using `int()`, as shown in the question I've linked. – mkrieger1 Jun 02 '21 at 17:44
  • 1
    @mkrieger1 Ha that was it! I just needed to put`x[1]['Buy']` in an `int()` function. Can't believe I overlooked the fact that it was returning a str and not an int. Thanks. – G T Jun 02 '21 at 17:48

0 Answers0