title: How to filter a data class in Kotlin multiple times
body: <p>I want to write functions that perform filters on a list of my data class/object. I can filter the classes but it seem like I need to create a new list every time a want to filter again, is there any way around this?</p>
  
  <p>I create multiple functions and extension function but it does not work</p>
  
  <p>My data class and custom list</p>
  
  <pre><code>data class Product(
      var name: String,
      var category: Category,
      var price: Double,
      var rating: Double
  )
  
  object ProductList {
      var productList = listOf(
          Product("Shopping Bag", Category.HOME, 11.75, 3.9),
          Product("Gold Earrings", Category.JEWELRY, 38.99, 4.2),
          Product("Golf Clubs", Category.SPORTS, 20.75, 4.1),
          Product("iPad", Category.ELECTRONICS, 180.75, 3.9),
          Product("MacBook Pro", Category.ELECTRONICS, 1200.85, 4.6),
          Product("Basketball Net", Category.SPORTS, 8.75, 3.5),
          Product("Lipstick", Category.WOMENS, 19.75, 4.1),
          Product("Dumbells", Category.HOME, 12.99, 4.8),
          Product("Gym Shoes", Category.MENS, 69.89, 3.9),
          Product("Coffee Mug", Category.HOME, 6.75, 3.9),
          Product("Reading Glasses", Category.MENS, 14.99, 2.8),
          Product("Nail Polish", Category.WOMENS, 8.50, 3.4),
          Product("Football Cleats", Category.SPORTS, 58.99, 3.9)
      )
  }
  </code></pre>
  
  <p>My two filtering functions</p>
  
  <pre><code>fun filterByCategory(category: Category): List&lt;Product&gt; {
      return productList.filter { it.category == category }
  }
  
  fun filterByRating(productList: List&lt;Product&gt;,rating: Double): List&lt;Product&gt; {
      return productList.filter { it.rating &gt;= rating }
  }
  </code></pre>
  
  <p>My sorting functions</p>
  
  <pre><code>fun sortByPriceLowToHigh(productList: List&lt;Product&gt;) {
      val sortedByPrice = productList.sortedBy { it.price  }
      for (i in sortedByPrice) {
          println("${i.name}: ${i.price}")
      }
  
  }
  
  
  fun sortByPriceHighToLow(productList: List&lt;Product&gt;) {
      val sortedByPrice = productList.sortedByDescending { it.price }
      for (i in sortedByPrice) {
          println("${i.name}: ${i.price}")
      }
  
  }
  </code></pre>
  
  <p>My function calls in main function</p>
  
  <pre><code>fun main(args: Array&lt;String&gt;) {
  
      val selectedCategory = filterByCategory(Category.HOME)
      filterByRating(selectedCategory, 4.0)
      println("Sorted by Price Low to High")
      sortByPriceLowToHigh(selectedCategory)
      println("")
      println("Sorted by Price High to Low")
      sortByPriceHighToLow(selectedCategory)
  
  }
  </code></pre>
  
  <p>I want my output to filter this list by category(which it does) then filter the list again by rating (which it does not)</p>
  
  <p>This is my output:</p>
  
  <pre><code>Sorted by Price Low to High
  Coffee Mug: 6.75
  Shopping Bag: 11.75
  Dumbells: 12.99
  
  Sorted by Price High to Low
  Dumbells: 12.99
  Shopping Bag: 11.75
  Coffee Mug: 6.75
  </code></pre>
  
  <p>The output I would like is:</p>
  
  <pre><code>Sorted by Price Low to High
  Dumbells: 12.99
  
  Sorted by Price High to Low
  Dumbells: 12.99
  </code></pre>
  
  <p>Because dumbbells is the only Product with a rating higher than 4.0 that is CATEGORY.HOME.
  I know I can just call filter on the list twice with a new predicate but I want to use function so I can make these calls in multiple places.</p>