By Lauren Hirsch and Adam Jourdan

NEW YORK/SHANGHAI (Reuters) - Wal-Mart Stores Inc <WMT.N> has sold its Chinese online grocery store in return for a stake in the country's no. 2 e-commerce firm, ripping up its previous strategy in efforts to cure ailing sales in one of the world's toughest retail markets.

The deal will see the U.S. grocery giant swap its Yihaodian platform for a 5 percent stake in JD.com Inc <JD.O>, worth about $1.5 billion by the firm's latest market value. The move also gives Wal-Mart a ringside seat in JD.com's bitter rivalry with Chinese e-commerce leader Alibaba Group Holding Ltd <BABA.N>.

The sell-off, announced on Monday, is a significant shift for Wal-Mart in China, where it operates more than 400 bricks-and-mortar stores. The firm has been shuttering underperforming outlets and grappling with soft online sales in the world's second-biggest economy since it bought full control of Yihaodian in July last year, saying the site would play a leading role in its China strategy.

"The reality is that e-commerce is hyper-competitive in China and it is tough for any platform to make money," said Ben Cavender, Shanghai-based principal of China Market Research Group. "Selling up in return for a 5 percent stake in JD.com is a good way of staying in the space while reducing the risk."

The deal echoes a strategy adopted by other international retailers and consumer goods makers - selling a local unit for a stake in a Chinese partner in order to prosper in a cut-throat marketplace. France's Danone SA <DANO.PA> sold its Dumex brand last year to raise its stake in local dairy giant China Mengniu Dairy Co Ltd <2319.HK>.

Wal-Mart's tie-up gives it access to JD.com's nationwide logistics and warehousing networks, as well as its over 150 million users - helping expand the U.S. firm's reach with China's increasingly tech-savvy middle class.

For JD.com, the deal could provide a boost in its intensifying competition in the fast-growing online grocery business with Alibaba - a market set to boom to nearly $180 billion by 2020 from $41 billion last year, according to data from food research body IGD.

Under the deal, JD.com will issue around 145 million new class A shares to Wal-Mart. JD.com will take ownership of Yihaodian, although the platform will continue to be operated by Wal-Mart.

The world's biggest retailer had previously talked up Yihaodian as playing a key role in turning around its China business. Wal-Mart has worked to turn around falling same-store sales in China, where the firm said in May it was facing a "challenging macroeconomic environment".

Analysts said that while Wal-Mart would give up a large amount of control and potential future profits from the business, the tie-up would allow the U.S. firm to concentrate on turning around its offline stores.

"It doesn't mean that (Wal-Mart has) pulled away, but to me it tells me they are trying to make smarter investments," said Edward Jones analyst Brian Yarbrough.

Wal-Mart's financial adviser on the deal was Morgan Stanley & Co LLC and its legal advisor was Morrison & Foerster LLP. JD.com's legal advisors were Orrick Herrington Sutcliffe LLP and Han Kun Law Offices.

(Additional reporting by Sruthi Ramakrishnan in BENGALURU, Rishika Sadam and Liana B. Baker in SAN FRANCISCO and Paul Carsten in BEIJING; Editing by Cynthia Osterman and Kenneth Maxwell)